【问题标题】:Android WebView File Handling crashing on cancel/backAndroid WebView 文件处理在取消/返回时崩溃
【发布时间】:2017-09-17 03:08:31
【问题描述】:

我正在使用另一篇文章中的以下代码来启用 HTML 表单输入(文件上传)中的文件处理。它目前与相机完美配合并从存储上传,但是如果我点击文件输入,然后不选择应用程序或点击返回,我会收到以下消息

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

我认为这与期望数据的文件选择器有关,但我返回一个空对象,因为没有通过 html 表单上传的图像但不知道如何修复它。我在这里尝试了该方法(Cancel a file upload in a Webview),但没有运气。我使用模拟器得到了完整的Android崩溃; if (resultCode != RESULT_OK) 等我认为是正确的。

完整的方法;

   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;
        }
   }

我正在使用 SDK 22、工具 25.0.0、minsdk 21、目标 22。

我仍在学习 Java,因此任何能解决我的问题的提示或示例将不胜感激。

谢谢

【问题讨论】:

    标签: java android webview


    【解决方案1】:

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

    【讨论】:

    • 是的,我想到了,我不知道如何处理?
    • 尝试将这一行“if (data != null || mCameraPhotoPath != null)”修改为“if (data != null && mCameraPhotoPath != null)”
    • 谢谢,我试过了,它已经停止崩溃,但是如果我点击取消并点击文件输入,它将不会再次打开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多