Yashdeep Patel
Yashdeep Patel

Reputation: 3130

for file picker openFileChooser() method not calling WebChromeClient of Kitkat 4.4

I have one hi-bride application in which one html page has file picker and i want to load that page in Android webview.

This pickers works well in Device browser but not in webview.

For to support this i am using one hidden method of WebChromeClient which is as below

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType){  
            /**updated, out of the IF **/
            mUploadMessage = uploadMsg;
            /**updated, out of the IF **/
            if(boolFileChooser){ //Take picture from filechooser
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                i.addCategory(Intent.CATEGORY_OPENABLE);  
                i.setType("image/*");  
                startActivityForResult( Intent.createChooser( i, "Pick File.." ), FILECHOOSER_RESULTCODE );  
            } else { //Take photo and upload picture
                Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
                photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
                if(photo.exists())
                    photo.delete();
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
                mCapturedImageURI = Uri.fromFile(photo);
                startActivityForResult(cameraIntent, CAMERAREQUEST_RESULTCODE);
            }
        }
    // Per Android < 3.0
        public void openFileChooser(ValueCallback<Uri> uploadMsg){
            openFileChooser(uploadMsg, "");
        }
        //Aftre
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            openFileChooser(uploadMsg, "");
        }

It was working fine till 4.3 but from 4.4 this method is not getting called. And they said https://code.google.com/p/android/issues/detail?id=62220 this has been removed.

Do anyone knows any alternate way. Please let me know your help will greatly appreciated

Upvotes: 7

Views: 2901

Answers (3)

Daniel
Daniel

Reputation: 1012

In Android 5.0, they introduced onShowFileChooser(), with which you can use an input form field in the webview and launch a file chooser to select images and files from the device.

Upvotes: 1

Assassin Shadow
Assassin Shadow

Reputation: 61

Bitmap bitmap;

private static final int READ_REQUEST_CODE = 42;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

    // Filter to only show results that can be "opened", such as a
    // file (as opposed to a list of contacts or timezones)
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Filter to show only images, using the image MIME data type.
    // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
    // To search for all documents available via installed storage providers,
    // it would be "*/*".
    intent.setType("image/*");

    startActivityForResult(intent, READ_REQUEST_CODE);

}


@Override
public void onActivityResult(int requestCode, int resultCode,
    Intent resultData) {

    // The ACTION_OPEN_DOCUMENT intent was sent with the request code
    // READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
    // response to some other intent, and the code below shouldn't run at all.

    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        // The document selected by the user won't be returned in the intent.
        // Instead, a URI to that document will be contained in the return intent
        // provided to this method as a parameter.
        // Pull that URI using resultData.getData().
        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();

            try {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             ImageView my_img_view = (ImageView ) findViewById (R.id.uploadlayout2);
             my_img_view.setImageBitmap(bitmap);                                        
        }
    }
}

Upvotes: -1

Yashdeep Patel
Yashdeep Patel

Reputation: 3130

There are no ways to openFileChooser method after 4.3 as google has removed that and they will come up with other way to handle this file chooser stuff in next version (Confirmed by Google engineer).

I moved to hybrid architecture to and write native function for file picker.

Upvotes: 4

Related Questions