Java – Images selected from the gallery are not set in ImageView

Images selected from the gallery are not set in ImageView… here is a solution to the problem.

Images selected from the gallery are not set in ImageView

I’m using a Motorola G3 device with Android 5.1.1 operating system.

When I try to pick a picture from the gallery and set it to ImageView, I get an exception. Here’s my code:

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

String selectedImageUri = getImageFromChooser(requestCode, resultCode, data);
        if (!selectedImageUri.equals("")) {
            Uri filePath = data.getData();
            try {
                Bitmap bitmap = BitmapFactory.decodeFile(selectedImageUri);
                ivAvatar.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 120, 120, false)); 
            } catch (Exception e) {
                e.printStackTrace();
            }

} else {
            Toast.makeText(getApplicationContext(), "Try Again!", Toast.LENGTH_LONG).show();
        }
}

I debugged the code and found that the URI is fine, the problem is initializing the Bitmap object.

Here’s the exception I encountered :

Please anyone help me with this :

01-01 12:12:16.074 27143-27143/com.wavi E/UploadItemActivity: idx :: 0
01-01 12:12:16.074 27143-27143/com.wavi E/selectedImageURI:ImagePhoto:-/storage/emulated/0/Pictures/1451279699474.jpg
01-01 12:12:16.076 27143-27143/com.wavi E/UploadItemActivity: Image Pathe ::  /storage/emulated/0/Pictures/1451279699474.jpg
01-01 12:12:16.078 27143-27143/com.wavi E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Pictures/1451279699474.jpg: open failed: EACCES (Permission denied)
01-01 12:12:16.078 27143-27143/com.wavi W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:617)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at com.wavi.profile.EditProfileActivity.onActivityResult(EditProfileActivity.java:201)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at android.app.Activity.dispatchActivityResult(Activity.java:6218)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at android.app.ActivityThread.deliverResults(ActivityThread.java:3655)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at android.app.ActivityThread.handleSendResult(ActivityThread.java:3702)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at android.app.ActivityThread.access$1300(ActivityThread.java:155)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at android.os.Handler.dispatchMessage(Handler.java:102)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at android.os.Looper.loop(Looper.java:135)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at android.app.ActivityThread.main(ActivityThread.java:5343)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at java.lang.reflect.Method.invoke(Native Method)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at java.lang.reflect.Method.invoke(Method.java:372)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
01-01 12:12:16.082 27143-27143/com.wavi W/System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:702)

Thanks!

Solution

Write permission

denied clearly in your logcat, you need to give READ permission in your manifest.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Related Problems and Solutions