简体   繁体   English

应用在图片选择时崩溃

[英]App crashes on Image Select

I want my user to pick an image, then it to be put in an image view, where I can process it. 我希望用户选择一个图像,然后将其放入图像视图中,以便在其中进行处理。 So far I have: 到目前为止,我有:

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


    }


}

Right now, I'm getting an error on this line: Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 现在,我在这条线上出现错误: Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); I'm puzzled. 我很困惑 I tried taking out the cursor and using selectedImage.getPath() and selectedImage.getEncodedPath() instead of using the picturePath string, but I still got an error. 我尝试取出游标并使用selectedImage.getPath()selectedImage.getEncodedPath()而不是使用picturePath字符串,但是仍然出现错误。 Where did I go wrong? 我哪里做错了? Is this a permissions-related issue? 这是与权限相关的问题吗?

EDIT: The stack trace: 编辑:堆栈跟踪:

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) 

You are getting back a null Uri . 您将返回一个null Uri

First, try getting rid of all those putExtra() calls from selectImage() . 首先,尝试摆脱所有来自selectImage() putExtra()调用。 Those are undocumented, unsupported, and may be causing your problem. 这些没有文档记录,不受支持,并且可能导致您的问题。 There are many image-cropping libraries that you can choose from to crop the image, if desired. 如果需要,可以选择许多图像裁剪库来裁剪图像。

I also recommend getting rid of the query() call in onActivityResult() . 我还建议摆脱onActivityResult()中的query()调用。 Add in an image-loading library and have it asynchronously load the image into your ImageView , given the Uri . 添加一个图像加载库,并在给定Uri使其异步将图像加载到ImageView Or, use openInputStream() on the ContentResolver , passing that stream to decodeStream() on BitmapFactory . 或者,在ContentResolver上使用openInputStream() ,将该流传decodeStream() BitmapFactory上的decodeStream()

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

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