简体   繁体   English

当“不”从文件选择/图库中选择图像时,应用程序崩溃

[英]App crashes when 'not' selecting an image from file choose / gallery

When I try to select an image the filechooser shows 'camera' and 'files', if I do not select one and click away, it crashes? 当我尝试选择图像时,文件选择器会显示“相机”和“文件”,如果我不选择图像并单击鼠标,它会崩溃吗? I have tried a few answers but I am a webdev not android dev. 我已经尝试了一些答案,但是我是一个webdev而不是android dev。 Help! 救命!

This is similar to app crashes when going back from gallery without selecting any image 这类似于从图库返回而未选择任何图像时应用崩溃

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

You haven't done any resultCode oriented check in your onActivityResult before doing your computation. 在进行计算之前,您尚未在onActivityResult进行任何面向resultCode的检查。 As when you come back without selecting the image, resultCode is RESULT_CANCELLED and your data is null. 当您不选择图像而返回时,resultCode为RESULT_CANCELLED而您的data为null。 Post that any operation on your data will always result in a null pointer exception 发布您对data任何操作将始终导致空指针异常

do as follows - 如下-

if (resultCode==RESULT_OK){
  // do stuff here that is the part of your try catch block
}

mostly your code is crashing when you try to do data.getDataString() on a data which is not there. 多数情况下,当您尝试对不存在的数据执行data.getDataString()时,代码将崩溃。 And also considering this is outside your try block it never gets caught and app crashes. 并且考虑到这在您的try块之外,它永远不会被捕获并且应用程序崩溃。

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

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