Joe S
Joe S

Reputation: 31

Android webview, openfilechooser termination

I have a simple application that gives the option to upload an image (gallery or camera), I used webview and managed to make it work.

I have a problem when I'm opening "openfilechoosr" dialog and coming back to my app without choosing any file (via the back or just clicking my app screen), than my app is not responding anymore (until android restart) and when I relaunch it, it appears as a blank page.

some more information:

The error I'm getting each time I'm trying to open the app after I got out the file chooser without choosing any file (the app will work again after restarting the android):

java.lang.Throwable: EventHub.removeMessages(int what = 107) is not supported before the WebViewCore is set up

My Code: [MainActivity]

webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSavePassword(false);
webView.getSettings().setSaveFormData(false);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new MyWebChromeViewClient());  
webView.loadUrl(url);


private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    private class MyWebChromeViewClient extends WebChromeClient {
        //@Override
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType )  {      
             File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
            // Create the storage directory if it does not exist
            if (! imageStorageDir.exists()){
                imageStorageDir.mkdirs();                  
            }
            File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");  
            imageUri = Uri.fromFile(file); 

            final List<Intent> cameraIntents = new ArrayList<Intent>();
            final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            final PackageManager packageManager = getPackageManager();
            final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
            for(ResolveInfo res : listCam) {
                final String packageName = res.activityInfo.packageName;
                final Intent intent = new Intent(captureIntent);
                intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                intent.setPackage(packageName);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                cameraIntents.add(intent);
            }


            uploadMessage = uploadMsg; 
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
            intent.addCategory(Intent.CATEGORY_OPENABLE);  
            intent.setType("image/*"); 
            Intent chooserIntent = Intent.createChooser(intent,"Image Chooser");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
            activity.startActivityForResult(chooserIntent,  FILECHOOSER_RESULTCODE); 
        }

Please advice.

Thanks Joe.

Upvotes: 1

Views: 5056

Answers (3)

alansiqueira27
alansiqueira27

Reputation: 8506

Return null on the callback, when the resultCode == 0 (back button)

mUploadMessage.onReceiveValue(null);

Upvotes: 0

OSP
OSP

Reputation: 1487

Also look at similar post here

Don't forget to handle Cancel in your dialog and tell WebChrome that selection fisnished.

Upvotes: 0

Joe S
Joe S

Reputation: 31

I Found the problem,

I forgot to add to the "onActivityResult" the case where the result is equals to "cancel" i.e. 0

Upvotes: 1

Related Questions