简体   繁体   中英

Image selected from gallary is not going to set in ImageView

I am using Motorola G3 device having OS Android 5.1.1.

When I am trying to pick a picture from gallery and set it to ImageView, am getting exception. Below is 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 have debug the code and came to know that the URI is coming properly and the problem is to initialize Bitmap object.

Here, is the exception I have got:

Please anyone help me to solve this problem:

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)

Thank you!

它显然写在你的logcat Permission denied ,你需要在你的清单中给予READ权限。

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

您需要在清单文件中提供读取权限。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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