Skip to content

Commit 19f891a

Browse files
committed
fix bug
1 parent 04436ed commit 19f891a

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import android.widget.FrameLayout;
1919
import android.provider.MediaStore;
2020
import androidx.core.content.FileProvider;
21+
import android.database.Cursor;
22+
import android.provider.OpenableColumns;
2123

2224
import java.util.List;
2325
import java.util.ArrayList;
@@ -43,6 +45,14 @@ class WebviewManager {
4345
private ValueCallback<Uri[]> mUploadMessageArray;
4446
private final static int FILECHOOSER_RESULTCODE=1;
4547
private Uri fileUri;
48+
private Uri videoUri;
49+
50+
private long getFileSize(Uri fileUri) {
51+
Cursor returnCursor = context.getContentResolver().query(fileUri, null, null, null, null);
52+
returnCursor.moveToFirst();
53+
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
54+
return returnCursor.getLong(sizeIndex);
55+
}
4656

4757
@TargetApi(7)
4858
class ResultHandler {
@@ -51,11 +61,12 @@ public boolean handleResult(int requestCode, int resultCode, Intent intent){
5161
if(Build.VERSION.SDK_INT >= 21){
5262
if(requestCode == FILECHOOSER_RESULTCODE){
5363
Uri[] results = null;
54-
if(resultCode == Activity.RESULT_OK){
55-
if(fileUri != null){
56-
results = new Uri[]{ fileUri };
57-
}
58-
if(intent != null){
64+
if (resultCode == Activity.RESULT_OK) {
65+
if (fileUri != null && getFileSize(fileUri) > 0) {
66+
results = new Uri[] { fileUri };
67+
} else if (videoUri != null && getFileSize(videoUri) > 0) {
68+
results = new Uri[] { videoUri };
69+
} else if (intent != null) {
5970
String dataString = intent.getDataString();
6071
if(dataString != null){
6172
results = new Uri[]{ Uri.parse(dataString) };
@@ -176,6 +187,8 @@ public boolean onShowFileChooser(
176187

177188
final String[] acceptTypes = getSafeAcceptedTypes(fileChooserParams);
178189
List<Intent> intentList = new ArrayList<Intent>();
190+
fileUri = null;
191+
videoUri = null;
179192
if (acceptsImages(acceptTypes)) {
180193
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
181194
fileUri = getOutputFilename(MediaStore.ACTION_IMAGE_CAPTURE);
@@ -184,13 +197,15 @@ public boolean onShowFileChooser(
184197
}
185198
if (acceptsVideo(acceptTypes)) {
186199
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
187-
fileUri = getOutputFilename(MediaStore.ACTION_VIDEO_CAPTURE);
188-
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
200+
videoUri = getOutputFilename(MediaStore.ACTION_VIDEO_CAPTURE);
201+
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
189202
intentList.add(takeVideoIntent);
190203
}
191204
Intent contentSelectionIntent;
192205
if (Build.VERSION.SDK_INT >= 21) {
206+
final boolean allowMultiple = fileChooserParams.getMode() == FileChooserParams.MODE_OPEN_MULTIPLE;
193207
contentSelectionIntent = fileChooserParams.createIntent();
208+
contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple);
194209
} else {
195210
contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
196211
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);

0 commit comments

Comments
 (0)