|
4 | 4 |
|
5 | 5 | package io.flutter.plugins.webviewflutter; |
6 | 6 |
|
| 7 | +import static io.flutter.plugins.webviewflutter.Constants.ACTION_FILE_CHOOSER_FINISHED; |
| 8 | +import static io.flutter.plugins.webviewflutter.Constants.EXTRA_FILE_URI; |
| 9 | +import static io.flutter.plugins.webviewflutter.Constants.EXTRA_SHOW_CAMERA_OPTION; |
| 10 | +import static io.flutter.plugins.webviewflutter.Constants.EXTRA_TITLE; |
| 11 | +import static io.flutter.plugins.webviewflutter.Constants.EXTRA_TYPE; |
| 12 | +import static io.flutter.plugins.webviewflutter.Constants.WEBVIEW_STORAGE_DIRECTORY; |
| 13 | + |
7 | 14 | import android.app.Activity; |
8 | 15 | import android.content.Intent; |
9 | 16 | import android.database.Cursor; |
|
12 | 19 | import android.provider.MediaStore; |
13 | 20 | import android.provider.OpenableColumns; |
14 | 21 | import android.util.Log; |
15 | | - |
16 | 22 | import androidx.annotation.Nullable; |
17 | 23 | import androidx.core.content.FileProvider; |
18 | | - |
19 | 24 | import java.io.File; |
20 | 25 | import java.io.FileOutputStream; |
21 | 26 | import java.io.IOException; |
|
24 | 29 | import java.text.SimpleDateFormat; |
25 | 30 | import java.util.Date; |
26 | 31 |
|
27 | | -import static io.flutter.plugins.webviewflutter.Constants.ACTION_FILE_CHOOSER_FINISHED; |
28 | | -import static io.flutter.plugins.webviewflutter.Constants.EXTRA_FILE_URI; |
29 | | -import static io.flutter.plugins.webviewflutter.Constants.EXTRA_SHOW_CAMERA_OPTION; |
30 | | -import static io.flutter.plugins.webviewflutter.Constants.EXTRA_TITLE; |
31 | | -import static io.flutter.plugins.webviewflutter.Constants.EXTRA_TYPE; |
32 | | -import static io.flutter.plugins.webviewflutter.Constants.WEBVIEW_STORAGE_DIRECTORY; |
33 | | - |
34 | 32 | public class FileChooserActivity extends Activity { |
35 | 33 |
|
36 | | - private static final int FILE_CHOOSER_REQUEST_CODE = 12322; |
37 | | - private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss"); |
38 | | - |
39 | | - private Uri cameraImageUri; |
40 | | - |
41 | | - @Override |
42 | | - protected void onCreate(@Nullable Bundle savedInstanceState) { |
43 | | - super.onCreate(savedInstanceState); |
44 | | - showFileChooser(getIntent().getBooleanExtra(EXTRA_SHOW_CAMERA_OPTION, false)); |
| 34 | + private static final int FILE_CHOOSER_REQUEST_CODE = 12322; |
| 35 | + private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss"); |
| 36 | + |
| 37 | + private Uri cameraImageUri; |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 41 | + super.onCreate(savedInstanceState); |
| 42 | + showFileChooser(getIntent().getBooleanExtra(EXTRA_SHOW_CAMERA_OPTION, false)); |
| 43 | + } |
| 44 | + |
| 45 | + private void showFileChooser(boolean enableCamera) { |
| 46 | + Intent galleryIntent = createGalleryIntent(); |
| 47 | + Intent takePictureIntent = enableCamera ? createCameraIntent() : null; |
| 48 | + if (galleryIntent == null && takePictureIntent == null) { |
| 49 | + // cannot open anything: cancel file chooser |
| 50 | + sendBroadcast(new Intent(ACTION_FILE_CHOOSER_FINISHED)); |
| 51 | + finish(); |
| 52 | + } else { |
| 53 | + Intent[] intentArray = |
| 54 | + takePictureIntent != null ? new Intent[] {takePictureIntent} : new Intent[] {}; |
| 55 | + |
| 56 | + Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); |
| 57 | + chooserIntent.putExtra( |
| 58 | + Intent.EXTRA_INTENT, galleryIntent != null ? galleryIntent : takePictureIntent); |
| 59 | + chooserIntent.putExtra(Intent.EXTRA_TITLE, getIntent().getStringExtra(EXTRA_TITLE)); |
| 60 | + chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); |
| 61 | + |
| 62 | + startActivityForResult(chooserIntent, FILE_CHOOSER_REQUEST_CODE); |
45 | 63 | } |
46 | | - |
47 | | - private void showFileChooser(boolean enableCamera) { |
48 | | - Intent galleryIntent = createGalleryIntent(); |
49 | | - Intent takePictureIntent = enableCamera ? createCameraIntent() : null; |
50 | | - if (galleryIntent == null && takePictureIntent == null) { |
51 | | - // cannot open anything: cancel file chooser |
52 | | - sendBroadcast(new Intent(ACTION_FILE_CHOOSER_FINISHED)); |
53 | | - finish(); |
54 | | - } else { |
55 | | - Intent[] intentArray = takePictureIntent != null ? new Intent[] { takePictureIntent } : new Intent[]{}; |
56 | | - |
57 | | - Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); |
58 | | - chooserIntent.putExtra(Intent.EXTRA_INTENT, galleryIntent != null ? galleryIntent : takePictureIntent); |
59 | | - chooserIntent.putExtra(Intent.EXTRA_TITLE, getIntent().getStringExtra(EXTRA_TITLE)); |
60 | | - chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); |
61 | | - |
62 | | - startActivityForResult(chooserIntent, FILE_CHOOSER_REQUEST_CODE); |
63 | | - } |
| 64 | + } |
| 65 | + |
| 66 | + private Intent createGalleryIntent() { |
| 67 | + Intent filesIntent = new Intent(Intent.ACTION_GET_CONTENT); |
| 68 | + filesIntent.setType(getIntent().getStringExtra(EXTRA_TYPE)); |
| 69 | + return (filesIntent.resolveActivity(getPackageManager()) != null) ? filesIntent : null; |
| 70 | + } |
| 71 | + |
| 72 | + private Intent createCameraIntent() { |
| 73 | + Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 74 | + if (takePictureIntent.resolveActivity(getPackageManager()) == null) { |
| 75 | + return null; |
64 | 76 | } |
| 77 | + // Create the File where the photo should go |
| 78 | + cameraImageUri = getTempImageUri(); |
| 79 | + takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImageUri); |
65 | 80 |
|
66 | | - private Intent createGalleryIntent() { |
67 | | - Intent filesIntent = new Intent(Intent.ACTION_GET_CONTENT); |
68 | | - filesIntent.setType(getIntent().getStringExtra(EXTRA_TYPE)); |
69 | | - return (filesIntent.resolveActivity(getPackageManager()) != null) ? filesIntent : null; |
70 | | - } |
71 | | - |
72 | | - private Intent createCameraIntent() { |
73 | | - Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
74 | | - if (takePictureIntent.resolveActivity(getPackageManager()) == null) { |
75 | | - return null; |
76 | | - } |
77 | | - // Create the File where the photo should go |
78 | | - cameraImageUri = getTempImageUri(); |
79 | | - takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImageUri); |
| 81 | + return takePictureIntent; |
| 82 | + } |
80 | 83 |
|
81 | | - return takePictureIntent; |
| 84 | + private File getStorageDirectory() { |
| 85 | + File imageDirectory = new File(getCacheDir(), WEBVIEW_STORAGE_DIRECTORY); |
| 86 | + if (!imageDirectory.exists() && !imageDirectory.mkdir()) { |
| 87 | + Log.e("WEBVIEW", "Unable to create storage directory"); |
82 | 88 | } |
83 | | - |
84 | | - private File getStorageDirectory() { |
85 | | - File imageDirectory = new File(getCacheDir(), WEBVIEW_STORAGE_DIRECTORY); |
86 | | - if (!imageDirectory.exists() && !imageDirectory.mkdir()) { |
87 | | - Log.e("WEBVIEW", "Unable to create storage directory"); |
| 89 | + return imageDirectory; |
| 90 | + } |
| 91 | + |
| 92 | + private Uri getTempImageUri() { |
| 93 | + String imageFileName = "IMG-" + simpleDateFormat.format(new Date()) + ".jpg"; |
| 94 | + File imageFile = new File(getStorageDirectory(), imageFileName); |
| 95 | + return FileProvider.getUriForFile( |
| 96 | + this, getApplicationContext().getPackageName() + ".generic.provider", imageFile); |
| 97 | + } |
| 98 | + |
| 99 | + private String getFileNameFromUri(Uri uri) { |
| 100 | + Cursor returnCursor = getContentResolver().query(uri, null, null, null, null); |
| 101 | + assert returnCursor != null; |
| 102 | + int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); |
| 103 | + returnCursor.moveToFirst(); |
| 104 | + String name = returnCursor.getString(nameIndex); |
| 105 | + returnCursor.close(); |
| 106 | + return name; |
| 107 | + } |
| 108 | + |
| 109 | + private Uri copyToLocalUri(Uri uri) { |
| 110 | + InputStream in = null; |
| 111 | + OutputStream out = null; |
| 112 | + try { |
| 113 | + File destination = new File(getStorageDirectory(), getFileNameFromUri(uri)); |
| 114 | + in = getContentResolver().openInputStream(uri); |
| 115 | + out = new FileOutputStream(destination); |
| 116 | + |
| 117 | + int cnt = 0; |
| 118 | + byte[] buffer = new byte[1024]; |
| 119 | + int len; |
| 120 | + while ((len = in.read(buffer)) != -1) { |
| 121 | + out.write(buffer, 0, len); |
| 122 | + cnt += len; |
| 123 | + } |
| 124 | + |
| 125 | + return FileProvider.getUriForFile( |
| 126 | + this, getApplicationContext().getPackageName() + ".generic.provider", destination); |
| 127 | + } catch (Exception e) { |
| 128 | + Log.e("WEBVIEW", "Unable to copy selected image", e); |
| 129 | + } finally { |
| 130 | + if (in != null) { |
| 131 | + try { |
| 132 | + in.close(); |
| 133 | + } catch (IOException e) { |
| 134 | + e.printStackTrace(); |
88 | 135 | } |
89 | | - return imageDirectory; |
90 | | - } |
91 | | - |
92 | | - private Uri getTempImageUri() { |
93 | | - String imageFileName = "IMG-" + simpleDateFormat.format(new Date()) + ".jpg"; |
94 | | - File imageFile = new File(getStorageDirectory(), imageFileName); |
95 | | - return FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".generic.provider", imageFile); |
96 | | - } |
97 | | - |
98 | | - private String getFileNameFromUri(Uri uri) { |
99 | | - Cursor returnCursor = getContentResolver().query(uri, null, null, null, null); |
100 | | - assert returnCursor != null; |
101 | | - int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); |
102 | | - returnCursor.moveToFirst(); |
103 | | - String name = returnCursor.getString(nameIndex); |
104 | | - returnCursor.close(); |
105 | | - return name; |
106 | | - } |
107 | | - |
108 | | - private Uri copyToLocalUri(Uri uri) { |
109 | | - InputStream in = null; |
110 | | - OutputStream out = null; |
| 136 | + } |
| 137 | + if (out != null) { |
111 | 138 | try { |
112 | | - File destination = new File(getStorageDirectory(), getFileNameFromUri(uri)); |
113 | | - in = getContentResolver().openInputStream(uri); |
114 | | - out = new FileOutputStream(destination); |
115 | | - |
116 | | - int cnt = 0; |
117 | | - byte[] buffer = new byte[1024]; |
118 | | - int len; |
119 | | - while ((len = in.read(buffer)) != -1) { |
120 | | - out.write(buffer, 0, len); |
121 | | - cnt += len; |
122 | | - } |
123 | | - |
124 | | - return FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".generic.provider", destination); |
125 | | - } catch(Exception e){ |
126 | | - Log.e("WEBVIEW", "Unable to copy selected image", e); |
127 | | - } finally { |
128 | | - if (in != null) { |
129 | | - try { |
130 | | - in.close(); |
131 | | - } catch (IOException e) { |
132 | | - e.printStackTrace(); |
133 | | - } |
134 | | - } |
135 | | - if (out != null) { |
136 | | - try { |
137 | | - out.close(); |
138 | | - } catch (IOException e) { |
139 | | - e.printStackTrace(); |
140 | | - } |
141 | | - } |
| 139 | + out.close(); |
| 140 | + } catch (IOException e) { |
| 141 | + e.printStackTrace(); |
142 | 142 | } |
143 | | - |
144 | | - return null; |
| 143 | + } |
145 | 144 | } |
146 | 145 |
|
147 | | - @Override |
148 | | - protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
149 | | - if (requestCode == FILE_CHOOSER_REQUEST_CODE) { |
150 | | - Intent intent = new Intent(ACTION_FILE_CHOOSER_FINISHED); |
151 | | - if (resultCode == Activity.RESULT_OK) { |
152 | | - if (data != null && data.getDataString() != null) { |
153 | | - // result from file browser |
154 | | - final Uri uri = copyToLocalUri(data.getData()); |
155 | | - intent.putExtra(EXTRA_FILE_URI, uri.toString()); |
156 | | - } else { |
157 | | - // result from camera |
158 | | - intent.putExtra(EXTRA_FILE_URI, cameraImageUri.toString()); |
159 | | - } |
160 | | - } |
161 | | - sendBroadcast(intent); |
162 | | - finish(); |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 151 | + if (requestCode == FILE_CHOOSER_REQUEST_CODE) { |
| 152 | + Intent intent = new Intent(ACTION_FILE_CHOOSER_FINISHED); |
| 153 | + if (resultCode == Activity.RESULT_OK) { |
| 154 | + if (data != null && data.getDataString() != null) { |
| 155 | + // result from file browser |
| 156 | + final Uri uri = copyToLocalUri(data.getData()); |
| 157 | + intent.putExtra(EXTRA_FILE_URI, uri.toString()); |
163 | 158 | } else { |
164 | | - super.onActivityResult(requestCode, resultCode, data); |
| 159 | + // result from camera |
| 160 | + intent.putExtra(EXTRA_FILE_URI, cameraImageUri.toString()); |
165 | 161 | } |
| 162 | + } |
| 163 | + sendBroadcast(intent); |
| 164 | + finish(); |
| 165 | + } else { |
| 166 | + super.onActivityResult(requestCode, resultCode, data); |
166 | 167 | } |
167 | | - |
| 168 | + } |
168 | 169 | } |
0 commit comments