应用程序在图像选择时崩溃

时间:2017-04-15 20:58:36

标签: java android android-permissions

我希望我的用户选择一个图像,然后将其放入图像视图中,我可以在其中处理它。到目前为止,我有:

public void selectImage(View view) {
    Intent intent = new Intent(Intent.ACTION_PICK,
            MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    intent.setType("image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("scale", true);
    intent.putExtra("outputX", 256);
    intent.putExtra("outputY", 256);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("return-data", true);
    startActivityForResult(intent, PICK_IMAGE);
}


//called at runtime
    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
        } else {
            selectImage(null);
        }
    }

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        selectImage(null);
    }
}

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

    final int RESULT_LOAD_IMAGE = 1;


    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.cImg);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));


    }


}

现在,我在这一行收到错误:Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 我很困惑。我尝试取出光标并使用selectedImage.getPath()selectedImage.getEncodedPath()而不是使用picturePath字符串,但我仍然遇到错误。我哪里做错了?这是与权限相关的问题吗?

修改 堆栈跟踪:

04-15 16:46:08.154 20055-20055/com.dannyyyy.isitabanana E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.dannyyyy.isitabanana, PID: 20055
                                                                          java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.dannyyyy.isitabanana/com.dannyyyy.isitabanana.MainActivity}: java.lang.NullPointerException: uri
                                                                              at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
                                                                              at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
                                                                              at android.app.ActivityThread.-wrap20(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                           Caused by: java.lang.NullPointerException: uri
                                                                              at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:111)
                                                                              at android.content.ContentResolver.query(ContentResolver.java:515)
                                                                              at android.content.ContentResolver.query(ContentResolver.java:474)
                                                                              at com.dannyyyy.isitabanana.MainActivity.onActivityResult(MainActivity.java:217)
                                                                              at android.app.Activity.dispatchActivityResult(Activity.java:6932)
                                                                              at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
                                                                              at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
                                                                              at android.app.ActivityThread.-wrap20(ActivityThread.java) 
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                              at android.os.Looper.loop(Looper.java:154) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                              at java.lang.reflect.Method.invoke(Native Method) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

1 个答案:

答案 0 :(得分:1)

您将返回null Uri

首先,尝试从putExtra()中删除所有selectImage()次来电。这些都没有记录,没有支持,可能会导致您的问题。如果需要,您可以选择许多image-cropping libraries来裁剪图像。

我还建议删除query()中的onActivityResult()来电。添加image-loading library,并在ImageView给出Uri的情况下将其异步加载到openInputStream()。或者,在ContentResolver上使用decodeStream(),将该流传递到BitmapFactory上的{{1}}。