简体   繁体   English

Android WebView文件处理在取消/返回时崩溃

[英]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). 我正在使用另一篇文章中的以下代码来启用HTML表单输入(文件上传)中的文件处理。 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. 我认为这与期望数据的文件选择器有关,但是由于没有图像要通过html格式上传,但我不返回错误,因此我将返回一个空对象。 I tried the method here ( Cancel a file upload in a Webview ) but no luck. 我在这里尝试了该方法( 取消Webview中的文件上传 ),但是没有运气。 I get a complete Android crash using the emulator using; 我在使用模拟器使用时遇到了完整的Android崩溃; if (resultCode != RESULT_OK) etc which I think is on the right path. 我认为正确的if (resultCode != RESULT_OK)等。

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. 我正在使用SDK 22,工具25.0.0,minsdk 21,目标22。

I'm still learning Java so any heads up or examples to fix my issue would be greatly appreciated. 我仍在学习Java,因此请多多注意或解决我的问题的示例。

Thanks 谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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