Android 4.0 在 imageView 中加载图像后崩溃

Android 4.0 crush after loading image in imageView

我是 Android Studio 的新手,我正在尝试制作一个简单的应用程序。我想要的只是从 Gallery 中选择一张图片并将其显示在 imageView 中。 这是我到现在为止得到的:

Activity:

package com.example.peyu.bellatrix_01b;

import android.content.Intent;  
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class ImageActivity extends AppCompatActivity {

public final static String TAG_LOG = ImageActivity.class.getSimpleName();
public static final int PICK_IMAGE = 100;
public Uri imageURI;
public ImageView imgPicture;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);

    Button btnAbrirImagen = (Button) findViewById(R.id.btnAbrirImagen);
    ImageView imgPicture = (ImageView) findViewById(R.id.imgPicture);

}

public void AbrirImagen(View v) {

    Intent photoPickerIntent= new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(photoPickerIntent, PICK_IMAGE);

}

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


    if(resultCode == RESULT_OK && requestCode == PICK_IMAGE){

        imageURI = data.getData();
        imgPicture.setImageURI(imageURI);

    }


}
}

我知道结果代码是 -1,但我不知道为什么...有什么想法吗?谢谢

这是目录:

09-01 22:34:50.145 20063-20063/com.example.peyu.bellatrix_01b W/dalvikvm: threadid=1: 线程退出并出现未捕获的异常 (group=0x41a53c98) 09-01 22:34:50.145 20063-20063/com.example.peyu.bellatrix_01b W/dalvikvm: threadid=1: 发生未捕获异常 09-01 22:34:50.146 20063-20063/com.example.peyu.bellatrix_01b W/System.err: java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request= 100, result=-1, data=Intent { dat=content://media/external/images/media/89391 flg=0x1 }} 到 activity {com.example.peyu.bellatrix_01b/com.example.peyu .bellatrix_01b.ImageActivity}: java.lang.NullPointerException 09-01 22:34:50.146 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3592) 09-01 22:34:50.146 20063-20063/com.example.peyu.bellatrix_01b W/System.错误:在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3635) 09-01 22:34:50.146 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 android.app.ActivityThread.access$1300(ActivityThread.java:151) 09-01 22:34:50.146 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390) 09-01 22:34:50.146 20063-20063/com.example.peyu.bellatrix_01b W/System.错误:在 android.os.Handler.dispatchMessage(Handler.java:110) 09-01 22:34:50.146 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 android.os.Looper.loop(Looper.java:193) 09-01 22:34:50.147 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 android.app.ActivityThread.main(ActivityThread.java:5333) 09-01 22:34:50.147 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 java.lang.reflect.Method.invokeNative(本机方法) 09-01 22:34:50.147 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 java.lang.reflect.Method.invoke(Method.java:515) 09-01 22:34:50.147 20063-20063/com.example.peyu.bellatrix_01b W/System.错误:在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 09-01 22:34:50.147 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 09-01 22:34:50.147 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 dalvik.system.NativeStart.main(本机方法) 09-01 22:34:50.147 20063-20063/com.example.peyu.bellatrix_01b W/System.err:由以下原因引起:java.lang.NullPointerException 09-01 22:34:50.150 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 com.example.peyu.bellatrix_01b.ImageActivity.onActivity结果(ImageActivity.java:70) 09-01 22:34:50.151 20063-20063/com.example.peyu.bellatrix_01b W/System.错误:在 android.app.Activity.dispatchActivityResult(Activity.java:5535) 09-01 22:34:50.151 20063-20063/com.example.peyu.bellatrix_01b W/System.err: 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3588) 09-01 22:34:50.151 20063-20063/com.example.peyu.bellatrix_01b W/System.err: ... 11 更多 09-01 22:34:50.151 20063-20063/com.example.peyu.bellatrix_01b W/dalvikvm: threadid=1: 调用 UncaughtExceptionHandler 09-01 22:34:50.157 20063-20063/com.example.peyu.bellatrix_01b E/AndroidRuntime:致命异常:main 进程:com.example.peyu.bellatrix_01b,PID:20063 java.lang.RuntimeException: 未能将结果 ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://media/external/images/media/89391 flg=0x1 }} 传送到 activity { com.example.peyu.bellatrix_01b/com.example.peyu.bellatrix_01b.ImageActivity}: java.lang.NullPointerException 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3592) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3635) 在 android.app.ActivityThread.access1300 美元(ActivityThread.java:151) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390) 在 android.os.Handler.dispatchMessage(Handler.java:110) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:5333) 在 java.lang.reflect.Method.invokeNative(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 在 dalvik.system.NativeStart.main(本机方法) 原因:java.lang.NullPointerException 在 com.example.peyu.bellatrix_01b.ImageActivity.onActivityResult(ImageActivity.java:70) 在 android.app.Activity.dispatchActivityResult(Activity.java:5535) 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3588) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:3635) 在 android.app.ActivityThread.access1300 美元(ActivityThread.java:151) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390) 在 android.os.Handler.dispatchMessage(Handler.java:110) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:5333) 在 java.lang.reflect.Method.invokeNative(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:515) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 在 dalvik.system.NativeStart.main(本机方法)

当您调用 ImageViewsetImageURI 时出现问题。 我建议你使用下面的这个库。该库将帮助您从该 imageURI 创建图像文件。

EasyImage

  1. 您正在声明一个 imageView,但没有初始化相同的 imageView。

  2. 您可能不知道新的 uri 权限模型。