简体   繁体   中英

Android WebView File Handling crashing on cancel/back

I'm using the following code from another post to enable file handling within a HTML form input (file upload). It currently works perfectly with the camera and uploading from storage, however if I click on the file input, then not select an application or click back I receive the following message

Attempt to invoke virtual method 'android.content.ClipData android.content.Intent.getClipData()' on a null object reference

I think this has something to do with the file chooser expecting data but I am returning a null object as there is no image to upload via html form but don't know how to fix it. I tried the method here ( Cancel a file upload in a Webview ) but no luck. I get a complete Android crash using the emulator using; if (resultCode != RESULT_OK) etc which I think is on the right path.

Method in full;

   public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode != INPUT_FILE_REQUEST_CODE || mUploadMessage == null) {
            super.onActivityResult(requestCode, resultCode, data);
            return;
        }
        try {
            String file_path = mCameraPhotoPath.replace("file:","");
            File file = new File(file_path);
            size = file.length();

        }catch (Exception e){
            Log.e("Error!", "Error while opening image file" + e.getLocalizedMessage());
        }

        if (data != null || mCameraPhotoPath != null) {
            Integer count = 1;
            ClipData images = null;
            try {
                images = data.getClipData();
            }catch (Exception e) {
                Log.e("Error!", e.getLocalizedMessage());
            }

            if (images == null && data != null && data.getDataString() != null) {
                count = data.getDataString().length();
            } else if (images != null) {
                count = images.getItemCount();
            }
            Uri[] results = new Uri[count];
            // Check that the response is a good one
            if (resultCode == Activity.RESULT_OK) {
                if (size != 0) {
                    // If there is not data, then we may have taken a photo
                    if (mCameraPhotoPath != null) {
                        results = new Uri[]{Uri.parse(mCameraPhotoPath)};
                    }
                } else if (data.getClipData() == null) {
                    results = new Uri[]{Uri.parse(data.getDataString())};
                } else {

                    for (int i = 0; i < images.getItemCount(); i++) {
                        results[i] = images.getItemAt(i).getUri();
                    }
                }
            }
            mUploadMessage.onReceiveValue(results);
            mUploadMessage = null;
        }
   }

I am using SDK 22, tools 25.0.0, minsdk 21, target 22.

I'm still learning Java so any heads up or examples to fix my issue would be greatly appreciated.

Thanks

我的猜测是,当您在其上调用getClipData()方法时,“数据”对象为NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM