|
2 | 2 |
|
3 | 3 | import android.app.Activity;
|
4 | 4 | import android.app.AlertDialog;
|
| 5 | +import android.content.DialogInterface; |
5 | 6 | import android.content.Intent;
|
6 | 7 | import android.content.res.AssetFileDescriptor;
|
7 | 8 | import android.net.Uri;
|
8 | 9 | import android.os.Bundle;
|
9 |
| -import android.os.Handler; |
10 |
| -import android.os.Message; |
11 | 10 | import android.provider.MediaStore;
|
| 11 | + |
12 | 12 | import java.io.File;
|
13 | 13 | import java.io.FileInputStream;
|
14 | 14 | import java.io.FileOutputStream;
|
15 | 15 |
|
16 | 16 | //Made By XFY9326
|
17 | 17 | //2017-10-12
|
18 | 18 |
|
19 |
| -public class MainActivity extends Activity |
20 |
| -{ |
21 |
| - private static final int RESULT_CODE = 1; |
22 |
| - private static final int MSG = 2; |
23 |
| - |
24 |
| - //false:send uri true:copy file |
25 |
| - private boolean file_mode = false; |
26 |
| - private File file; |
27 |
| - private AlertDialog load; |
28 |
| - private Intent asset_intent; |
29 |
| - private Handler process = new Handler(){ |
30 |
| - |
31 |
| - @Override |
32 |
| - public void handleMessage(Message msg) |
33 |
| - { |
34 |
| - if (msg.what == MSG) |
35 |
| - { |
36 |
| - load.cancel(); |
37 |
| - setResult(RESULT_OK, asset_intent); |
38 |
| - finish(); |
39 |
| - System.gc(); |
40 |
| - |
41 |
| - } |
42 |
| - super.handleMessage(msg); |
43 |
| - } |
44 |
| - |
45 |
| - }; |
| 19 | +public class MainActivity extends Activity { |
| 20 | + private static final int RESULT_CODE = 1; |
| 21 | + //false:send uri true:copy file |
| 22 | + private boolean file_mode = false; |
| 23 | + private File file; |
| 24 | + private AlertDialog load; |
| 25 | + private Intent asset_intent; |
46 | 26 |
|
47 | 27 | @Override
|
48 |
| - protected void onCreate(Bundle savedInstanceState) |
49 |
| - { |
| 28 | + protected void onCreate(Bundle savedInstanceState) { |
50 | 29 | super.onCreate(savedInstanceState);
|
51 |
| - startSelect(); |
| 30 | + startSelect(); |
52 | 31 | }
|
53 | 32 |
|
54 |
| - @Override |
55 |
| - protected void onActivityResult(int requestCode, int resultCode, Intent data) |
56 |
| - { |
57 |
| - if (requestCode == RESULT_CODE) |
58 |
| - { |
59 |
| - if (data != null) |
60 |
| - { |
61 |
| - asset_intent = data; |
62 |
| - if (file_mode) |
63 |
| - { |
64 |
| - loading(); |
65 |
| - saveMediaToFile(); |
66 |
| - } |
67 |
| - else |
68 |
| - { |
69 |
| - setResult(RESULT_OK, data); |
70 |
| - finish(); |
71 |
| - System.gc(); |
72 |
| - } |
73 |
| - } |
74 |
| - } |
75 |
| - super.onActivityResult(requestCode, resultCode, data); |
76 |
| - } |
| 33 | + @Override |
| 34 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 35 | + if (requestCode == RESULT_CODE) { |
| 36 | + if (data != null) { |
| 37 | + asset_intent = data; |
| 38 | + if (file_mode) { |
| 39 | + loading(); |
| 40 | + saveMediaToFile(); |
| 41 | + } else { |
| 42 | + setResult(RESULT_OK, data); |
| 43 | + finish(); |
| 44 | + System.gc(); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + super.onActivityResult(requestCode, resultCode, data); |
| 49 | + } |
77 | 50 |
|
78 |
| - //Open picture or video selector |
79 |
| - private void startSelect() |
80 |
| - { |
81 |
| - Intent base_intent = getIntent(); |
82 |
| - Intent new_intent = new Intent(); |
83 |
| - Uri uri = base_intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT); |
84 |
| - if (uri != null) |
85 |
| - { |
86 |
| - file_mode = true; |
87 |
| - file = new File(UriMethod.getUriAbsolutePath(this, uri)); |
88 |
| - } |
89 |
| - if (base_intent.getAction().equals(MediaStore.ACTION_IMAGE_CAPTURE) || base_intent.getAction().equals(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)) |
90 |
| - { |
91 |
| - new_intent.setType("image/*"); |
92 |
| - } |
93 |
| - else if (base_intent.getAction().equals(MediaStore.ACTION_VIDEO_CAPTURE)) |
94 |
| - { |
95 |
| - new_intent.setType("video/*"); |
96 |
| - } |
97 |
| - new_intent.setAction(Intent.ACTION_GET_CONTENT); |
98 |
| - startActivityForResult(new_intent, RESULT_CODE); |
99 |
| - } |
| 51 | + //Open picture or video selector |
| 52 | + private void startSelect() { |
| 53 | + Intent base_intent = getIntent(); |
| 54 | + Intent new_intent = new Intent(); |
| 55 | + Uri uri = base_intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT); |
| 56 | + if (uri != null) { |
| 57 | + file_mode = true; |
| 58 | + file = new File(UriMethod.getUriAbsolutePath(this, uri)); |
| 59 | + } |
| 60 | + if (base_intent.getAction().equals(MediaStore.ACTION_IMAGE_CAPTURE) || base_intent.getAction().equals(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)) { |
| 61 | + new_intent.setType("image/*"); |
| 62 | + } else if (base_intent.getAction().equals(MediaStore.ACTION_VIDEO_CAPTURE)) { |
| 63 | + new_intent.setType("video/*"); |
| 64 | + } |
| 65 | + new_intent.setAction(Intent.ACTION_GET_CONTENT); |
| 66 | + startActivityForResult(new_intent, RESULT_CODE); |
| 67 | + } |
100 | 68 |
|
101 |
| - private void loading() |
102 |
| - { |
103 |
| - AlertDialog.Builder dialog = new AlertDialog.Builder(this); |
104 |
| - dialog.setTitle(R.string.loading); |
105 |
| - dialog.setMessage(getString(R.string.loading_msg) + file.getAbsolutePath().toString()); |
106 |
| - dialog.setCancelable(false); |
107 |
| - load = dialog.show(); |
108 |
| - } |
| 69 | + private void loading() { |
| 70 | + AlertDialog.Builder dialog = new AlertDialog.Builder(this); |
| 71 | + dialog.setTitle(R.string.loading); |
| 72 | + dialog.setMessage(getString(R.string.loading_msg) + file.getAbsolutePath()); |
| 73 | + dialog.setCancelable(false); |
| 74 | + dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { |
| 75 | + @Override |
| 76 | + public void onCancel(DialogInterface dialog) { |
| 77 | + setResult(RESULT_OK, asset_intent); |
| 78 | + finish(); |
| 79 | + System.gc(); |
| 80 | + } |
| 81 | + }); |
| 82 | + load = dialog.show(); |
| 83 | + } |
109 | 84 |
|
110 |
| - private void saveMediaToFile() |
111 |
| - { |
112 |
| - new Thread(new Runnable(){ |
113 |
| - public void run() |
114 |
| - { |
115 |
| - Uri uri = asset_intent.getData(); |
116 |
| - try |
117 |
| - { |
118 |
| - AssetFileDescriptor afd = getContentResolver().openAssetFileDescriptor(uri, "r"); |
119 |
| - if (file != null) |
120 |
| - { |
121 |
| - FileInputStream in = afd.createInputStream(); |
122 |
| - FileOutputStream out = new FileOutputStream(file); |
123 |
| - byte[] buff = new byte[1024]; |
124 |
| - int len; |
125 |
| - while ((len = in.read(buff)) > 0) |
126 |
| - { |
127 |
| - out.write(buff, 0, len); |
128 |
| - } |
129 |
| - in.close(); |
130 |
| - out.flush(); |
131 |
| - out.close(); |
132 |
| - } |
133 |
| - } |
134 |
| - catch (Exception e) |
135 |
| - { |
136 |
| - e.printStackTrace(); |
137 |
| - } |
138 |
| - process.sendEmptyMessage(MSG); |
139 |
| - } |
140 |
| - }).start(); |
141 |
| - } |
| 85 | + private void saveMediaToFile() { |
| 86 | + new Thread(new Runnable() { |
| 87 | + public void run() { |
| 88 | + Uri uri = asset_intent.getData(); |
| 89 | + try { |
| 90 | + AssetFileDescriptor afd = getContentResolver().openAssetFileDescriptor(uri, "r"); |
| 91 | + if (file != null && afd != null) { |
| 92 | + FileInputStream in = afd.createInputStream(); |
| 93 | + FileOutputStream out = new FileOutputStream(file); |
| 94 | + byte[] buff = new byte[1024]; |
| 95 | + int len; |
| 96 | + while ((len = in.read(buff)) > 0) { |
| 97 | + out.write(buff, 0, len); |
| 98 | + } |
| 99 | + in.close(); |
| 100 | + out.flush(); |
| 101 | + out.close(); |
| 102 | + } |
| 103 | + } catch (Exception e) { |
| 104 | + e.printStackTrace(); |
| 105 | + } |
| 106 | + load.cancel(); |
| 107 | + } |
| 108 | + }).start(); |
| 109 | + } |
142 | 110 |
|
143 | 111 | }
|
0 commit comments