2 years ago (2017-02-21 06:25:49)

Upload Image/File from Gallery or Camera in WebView in Android

11 9
24.5k

This function works in all versions of android OS webview except 4.4.2 (specifically). And don't worry about that because even google can't give you solution for that and the reason is; they intentionally removed the function in Kitkat 4.4.2, yeah, really a bad move.

UPDATED: Read Android Smart WebView with advanced features including Image/File Upload methods

Well, here's is the overview how to Upload Image or File in Android WebView.

public void openFileChooser(ValueCallback uploadMsg){
     mUM = uploadMsg;
     Intent i = new Intent(Intent.ACTION_GET_CONTENT);
     i.addCategory(Intent.CATEGORY_OPENABLE);
     i.setType("image/*");
     MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FCR);
}

The above method works best for Android 3.0+ but even in some devices the very same method doesn't works. That's how bad it is, but still not hard at all. And this is what we use for Android 5.0+ (including Lollipop and Marshmallow).

public boolean onShowFileChooser(
     WebView webView, ValueCallback<Uri[]> filePathCallback,
     WebChromeClient.FileChooserParams fileChooserParams){
     if(mUMA != null){
          mUMA.onReceiveValue(null);
     }
     mUMA = filePathCallback;
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){
          File photoFile = null;
          try{
               photoFile = createImageFile();
               takePictureIntent.putExtra("PhotoPath", mCM);
          }catch(IOException ex){
               Log.e(TAG, "Image file creation failed", ex);
          }
          if(photoFile != null){
               mCM = "file:" + photoFile.getAbsolutePath();
               takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
          }else{
               takePictureIntent = null;
          }
     }
     Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
     contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
     contentSelectionIntent.setType("image/*");
     Intent[] intentArray;
     if(takePictureIntent != null){
          intentArray = new Intent[]{takePictureIntent};
     }else{
          intentArray = new Intent[0];
     }

     Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
     chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
     chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
     startActivityForResult(chooserIntent, FCR);
     return true;
}

Create image file function, as mentioned in the above code we need this to create new temp file to upload.

private File createImageFile() throws IOException{
     @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
     String imageFileName = "img_"+timeStamp+"_";
     File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
     return File.createTempFile(imageFileName,".jpg",storageDir);
}

Here's the complete project on GitHub to view and download:

Open GitHub

For further help, message me or start a thread on this note.

Happy Coding :)

















































Sign up or Sign in for free.
And start a new thread.
an14875102092851
Hi, when i use this code, android is directly opening the "open from" interface where i can choose existing files on the smartphone. it doesn't provide me the option to take the camera app to upload an image. tested that on various phones and virtual devices. what am i doing wrong? is there a missing permission?
an14875102092851
update: with android sdk 22 it works, with sdk 25 it doesn't. does anybody know a solution for that problem? please help me. michael
mgks
who's michael?
10 months ago   0 Report Permalink[S]
mgks
I've already added an update to the project, it explains support for all devices; except 4.4 as Google has stopped updating it and left it buggy.
Here it is - https://infeeds.com/d/CODEmgks/25019/android-smart-webview-open-source-upload

And I don't know what you've wrote in your codes, so, no idea but permissions can be the issue, check for WRITE_EXTERNAL_STORAGE also.
2 years ago   0 Report Permalink[S]
mochadwi
awesome works ^_^

Thank you for providing this great source code :))) With this, we can continue improving and learning the new things :)))
mgks
I'm happy to be helpful!
2 years ago   0 Report Permalink[S]
kopa
helped me a lot.
mgks
I feel honoured.
10 months ago   0 Report Permalink[S]
More notes by Ghazi Khan