java.lang.RuntimeException:无法交付结果***原因:java.lang.NullPointerException:uri

时间:2019-04-06 17:39:52

标签: android nullpointerexception uri

我已经看过-我找不到与该问题有关的URI的其他任何内容。

我的应用程序允许用户从图库中选择图像,然后该应用程序接收uri,将其转换为位图,然后将其压缩为JPEG,并将文件存储在cacheDir中以备后用。 选择图像后,应用程序“停止工作”。

我知道添加resultCode!= RESULT_CANCELED,并且检查数据是否为null可能会解决最初的崩溃问题,但是我认为这不会解决整个问题-不管是什么。 我仍在学习Java-我被开发人员所吸引,因此被迫自己开发应用程序,以便尽我所能学习,如果代码中有任何我可以改进的地方,请告诉我,谢谢。

从图库中选择图片:

public void selectImageFromGallery(){
    Intent chooserIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(chooserIntent, 1);
}

接收数据,在活动结果中获取URI,发送到网络功能:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK && requestCode == IMAGE_PICK  || requestCode == VIDEO_PICK) {
        switch (requestCode) {
            case 1:
                Intent intent = getIntent();
                setResult(RESULT_OK, intent);
                Uri imageUri = intent.getData();
                try {
                    startSendImage(imageUri);
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }


                //Toast.makeText(this, "Sending uri to startSendImage", Toast.LENGTH_SHORT).show();
                //uriData = data.getData();
                //startSendImage(uriData);

                break;

            case 2:


                break;
            default:
                break;
        }
    }
}

准备图像供以后使用:

public void startSendImage (Uri imageUri) throws IOException {
    Toast.makeText(this, "Received. Copying image.", Toast.LENGTH_SHORT).show();
    //Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);

//create a file to write bitmap data
    String filename = "temp.jpg";
    File f = new File(this.getCacheDir(), filename);
    f.createNewFile();

//Convert bitmap to byte array
    //Bitmap bitmap1 = bitmap;
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();

//write the bytes in file
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(bitmapdata);
    fos.flush();
    fos.close();
    if (f.exists()){
        Toast.makeText(this, "File Exists", Toast.LENGTH_SHORT).show();
    }
}

日志猫:

2019-04-06 18:20:43.907 18116-18116/com.com.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.com.project, PID: 18116
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/1598 flg=0x1 (has extras) }} to activity {com.com.project/com.com.project.MainActivity}: java.lang.NullPointerException: uri
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4491)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4534)
    at android.app.ActivityThread.-wrap20(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1752)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: java.lang.NullPointerException: uri
    at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:128)
    at android.content.ContentResolver.openInputStream(ContentResolver.java:956)
    at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:952)
    at com.com.project.MainActivity.startSendImage(MainActivity.java:138)
    at com.com.project.MainActivity.onActivityResult(MainActivity.java:102)
    at android.app.Activity.dispatchActivityResult(Activity.java:7556)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4487)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4534) 
    at android.app.ActivityThread.-wrap20(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1752) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6944) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

0 个答案:

没有答案