0

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); 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. 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) 
Alex Jone
  • 292
  • 2
  • 14

1 Answers1

1

You are getting back a null Uri.

First, try getting rid of all those putExtra() calls from selectImage(). 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(). Add in an image-loading library and have it asynchronously load the image into your ImageView, given the Uri. Or, use openInputStream() on the ContentResolver, passing that stream to decodeStream() on BitmapFactory.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'll try. Are there any specific `putExtra()`s that I can keep? I'll take them all away for now, but they would be useful in my app. – Alex Jone Apr 15 '17 at 21:26
  • @AlexJone: Get rid of all of them, as there is no requirement for any `ACTION_PICK` (or anything else) to support them. As I wrote in my answer, use an image-cropping library. – CommonsWare Apr 15 '17 at 21:29
  • Thank you very much, it worked just by taking out the `putExtras()`. – Alex Jone Apr 15 '17 at 21:31