Skip to content
This repository has been archived by the owner on Nov 28, 2021. It is now read-only.

Commit

Permalink
Android Studio格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
XFY9326 committed Oct 22, 2017
1 parent 8d9d491 commit d1187c5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 61 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<action android:name="android.media.action.VIDEO_CAPTURE" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Expand Down
119 changes: 60 additions & 59 deletions app/src/main/java/tool/xfy9326/selectmedia/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand All @@ -30,28 +31,28 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == RESULT_CODE && data != null) {
if (file_mode) {
loading(data);
saveMediaToFile(data);
} else {
exit(data);
}
}
} else {
onBackPressed();
}
if (resultCode == RESULT_OK) {
if (requestCode == RESULT_CODE && data != null) {
if (file_mode) {
loading(data);
saveMediaToFile(data);
} else {
exit(data);
}
}
} else {
onBackPressed();
}
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onBackPressed() {
setResult(RESULT_CANCELED);
finish();
System.gc();
super.onBackPressed();
}
@Override
public void onBackPressed() {
setResult(RESULT_CANCELED);
finish();
System.gc();
super.onBackPressed();
}

//Open picture or video selector
private void startSelect() {
Expand All @@ -60,13 +61,13 @@ private void startSelect() {
Uri uri = base_intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
if (uri != null) {
file_mode = true;
try {
String path = UriMethod.getUriAbsolutePath(this, uri);
select_file = new File(path);
} catch (IllegalArgumentException e) {
e.printStackTrace();
file_mode = false;
}
try {
String path = UriMethod.getUriAbsolutePath(this, uri);
select_file = new File(path);
} catch (IllegalArgumentException e) {
e.printStackTrace();
file_mode = false;
}
}
if (base_intent.getAction().equals(MediaStore.ACTION_IMAGE_CAPTURE) || base_intent.getAction().equals(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)) {
new_intent.setType("image/*");
Expand All @@ -80,47 +81,47 @@ private void startSelect() {
private void loading(final Intent asset_intent) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.loading);
dialog.setMessage(getString(R.string.loading_msg) + select_file.getAbsolutePath());
dialog.setMessage(getString(R.string.loading_msg) + select_file.getAbsolutePath());
dialog.setCancelable(false);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
exit(asset_intent);
}
});
@Override
public void onCancel(DialogInterface dialog) {
exit(asset_intent);
}
});
load = dialog.show();
}

private void saveMediaToFile(final Intent asset_intent) {
new Thread(new Runnable() {
public void run() {
Uri uri = asset_intent.getData();
try {
AssetFileDescriptor afd = getContentResolver().openAssetFileDescriptor(uri, "r");
if (afd != null && select_file != null) {
FileInputStream in = afd.createInputStream();
FileOutputStream out = new FileOutputStream(select_file);
byte[] buff = new byte[1024];
int len;
while ((len = in.read(buff)) > 0) {
out.write(buff, 0, len);
}
in.close();
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
load.cancel();
}
}).start();
public void run() {
Uri uri = asset_intent.getData();
try {
AssetFileDescriptor afd = getContentResolver().openAssetFileDescriptor(uri, "r");
if (afd != null && select_file != null) {
FileInputStream in = afd.createInputStream();
FileOutputStream out = new FileOutputStream(select_file);
byte[] buff = new byte[1024];
int len;
while ((len = in.read(buff)) > 0) {
out.write(buff, 0, len);
}
in.close();
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
load.cancel();
}
}).start();
}

private void exit(Intent asset_intent) {
setResult(RESULT_OK, asset_intent);
finish();
System.gc();
}
private void exit(Intent asset_intent) {
setResult(RESULT_OK, asset_intent);
finish();
System.gc();
}

}
3 changes: 1 addition & 2 deletions app/src/main/java/tool/xfy9326/selectmedia/UriMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ private static String getDataColumn(Context context, Uri uri, String selection,
int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
}
finally {
} finally {
if (cursor != null)
cursor.close();
}
Expand Down

0 comments on commit d1187c5

Please sign in to comment.