Search code examples
androidandroid-gallery

setImageDrawable did not set image on API23


I'm trying to set an image to my CircleImageView on Android API 23. I get the image path from the device's memory and store this string path to database:

<de.hdodenhof.circleimageview.CircleImageView
  android:id="@+id/fragment_edit_picture"
  android:layout_width="150dp"
  android:layout_height="150dp"
  android:layout_gravity="center"
  android:layout_margin="5dp"
  android:src="@mipmap/ic_launcher"
  android:onClick="selectFromGallery"
  app:civ_border_width="2dp"
/>

It is working on API 19 and before, but it's not working on a virtual device in Genymotion with Android version API 23. The selected image is not showing in the ImageView. I'm setting the image to the ImageView this way:

mCurrentImagePath = FileUtils.getPath(getActivity(), selectedImageUri);
Log.d(TAG, "mCurrentImagePath: " + mCurrentImagePath);

// Update client's picture
if (mCurrentImagePath != null && !mCurrentImagePath.isEmpty()) {
    fragmentEditPicture.setImageDrawable(new BitmapDrawable(getResources(),
            FileUtils.getResizedBitmap(mCurrentImagePath, 512, 512)));
}

The image path is mCurrentImagePath: /storage/emulated/0/Download/56836_(5-27-2006 2-28-40)(1920x1200).JPG.

Do you know why this isn't working?


Solution

  • There is a problem with the method you are using FileUtils.getResizedBitmap(). I created a sample project to reproduce your issue. It's working fine in that sample project. When using the code snippet below, you shouldn't have a problem loading images from the device's memory.

    public static Bitmap getResizedBitmap(String imagePath, int width, int height) {
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
        // Recycling the bitmap, because it's already used and not needed anymore
        bitmap.recycle();
        return resizedBitmap;
    }