From e6446217a88eacc858d7fb4818984aa8ccd787ef Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Wed, 7 Sep 2016 23:55:25 +0200 Subject: [PATCH] Fixed image upload on 4.2 devices --- app/src/main/AndroidManifest.xml | 2 +- .../activity/MainActivity.java | 100 ++++++++++-------- 2 files changed, 56 insertions(+), 46 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8a35ba68..a4b02fa7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -52,7 +52,7 @@ mFilePathCallback; + private ValueCallback imageUploadFilePathCallbackNew; + private ValueCallback imageUploadFilePathCallbackOld; private String mCameraPhotoPath; private WebSettings webSettings; private AppSettings appSettings; @@ -353,28 +353,16 @@ public class MainActivity extends AppCompatActivity } //For Android 4.1/4.2 only. DONT REMOVE - protected void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) + protected void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { - Log.d(App.TAG, "openFileChooser(ValCallback, String, String"); - mFilePathCallback = uploadMsg; + Log.d(App.TAG, "openFileChooser(ValCallback, String, String"); + imageUploadFilePathCallbackOld = uploadMsg; Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); intent.putExtra("return-data", true); - startActivityForResult(Intent.createChooser(intent, "Select Picture"),INPUT_FILE_REQUEST_CODE); - } - //For Android 4.1/4.2 only. DONT REMOVE - protected void openFileChooser(ValueCallback uploadMsg) - { - Log.d(App.TAG, "openFileChooser(ValCallback"); - onShowFileChooser(webView, uploadMsg, null); - /* - mUploadMessage = uploadMsg; - Intent i = new Intent(Intent.ACTION_GET_CONTENT); - i.addCategory(Intent.CATEGORY_OPENABLE); - i.setType("image/*"); - startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); - */ + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + startActivityForResult(Intent.createChooser(intent, "Select Picture"), INPUT_FILE_REQUEST_CODE_OLD); } @Override @@ -404,8 +392,8 @@ public class MainActivity extends AppCompatActivity } Log.d(App.TAG, "onOpenFileChooser"); - if (mFilePathCallback != null) mFilePathCallback.onReceiveValue(null); - mFilePathCallback = filePathCallback; + if (imageUploadFilePathCallbackNew != null) imageUploadFilePathCallbackNew.onReceiveValue(null); + imageUploadFilePathCallbackNew = filePathCallback; Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { @@ -448,7 +436,7 @@ public class MainActivity extends AppCompatActivity chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); Log.d(App.TAG,"startActivityForResult"); - startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE); + startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE_NEW); return true; } }); @@ -581,31 +569,53 @@ public class MainActivity extends AppCompatActivity @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(App.TAG,"onActivityResult:"); - if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) { - Log.d(App.TAG,"reqCode != INPUT_FILE_REQUEST_CODE or mFilePathCallback == null"); - super.onActivityResult(requestCode, resultCode, data); - return; - } - Uri[] results = null; - if (resultCode == Activity.RESULT_OK) { - Log.d(App.TAG, "Activity.RESULT_OK"); - if (data == null) { - Log.d(App.TAG, "data == null"); - if (mCameraPhotoPath != null) { - Log.d(App.TAG, "mCameraPhotoPath != null"); - results = new Uri[]{Uri.parse(mCameraPhotoPath)}; + switch (requestCode) { + case INPUT_FILE_REQUEST_CODE_NEW: { + Log.d(App.TAG,"INPUT_FILE_REQUEST_CODE_NEW:"); + if (imageUploadFilePathCallbackNew == null || resultCode != Activity.RESULT_OK) { + Log.e(App.TAG, "Callback is null: " + (imageUploadFilePathCallbackNew == null) + + " resultCode: " + resultCode); + return; } - } else { - Log.d(App.TAG, "data != null"); - String dataString = data.getDataString(); - if (dataString != null) { - Log.d(App.TAG, "dataString != null"); - results = new Uri[]{Uri.parse(dataString)}; + Uri[] results = null; + if (data == null) { + if (mCameraPhotoPath != null) { + results = new Uri[]{Uri.parse(mCameraPhotoPath)}; + } + } else { + String dataString = data.getDataString(); + if (dataString != null) { + results = new Uri[]{Uri.parse(dataString)}; + } } + imageUploadFilePathCallbackNew.onReceiveValue(results); + imageUploadFilePathCallbackNew = null; + return; + } + case INPUT_FILE_REQUEST_CODE_OLD: { + Log.d(App.TAG,"INPUT_FILE_REQUEST_CODE_OLD:"); + if (imageUploadFilePathCallbackOld == null || resultCode != Activity.RESULT_OK) { + Log.e(App.TAG, "Callback is null: " + (imageUploadFilePathCallbackOld == null) + + " resultCode: " + resultCode); + return; + } + Uri results = null; + if (data == null) { + if (mCameraPhotoPath != null) { + results = Uri.parse(mCameraPhotoPath); + } + } else { + String dataString = data.getDataString(); + if (dataString != null) { + results = Uri.parse(dataString); + } + } + imageUploadFilePathCallbackOld.onReceiveValue(results); + imageUploadFilePathCallbackOld = null; + return; } - mFilePathCallback.onReceiveValue(results); - mFilePathCallback = null; } + super.onActivityResult(requestCode, resultCode, data); } @Override