1

I have been working hard trying to upload files from a webview. I tried many solutions but none worked.

http://m0s-programming.blogspot.tw/2011/02/file-upload-in-through-webview-on.html

Android WebView File Upload

filechooser works fine in a desktop browser like chrome when I click the button and the browser opens a dialog box where I can choose a file to upload.

When I use my cellphone to test the program and I click choose file the Dialog box has no reaction

Can someone help me? Thanks

1 Answer 1

1

Refer this Documentation :

https://infeeds.com/d/CODEmgks/20475/upload-image-file-gallery-or-camera-webv

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

there is git project available in the link
@Anymore : Upvote the Answer for others to refer easily with it
it says my reputation is less than 15 so do not change the publicly displayed post score

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.