当前位置: 代码迷 >> Android >> setImageDrawable没有在API23上设置图像
  详细解决方案

setImageDrawable没有在API23上设置图像

热度:42   发布时间:2023-08-04 11:10:25.0

我正在尝试将图像设置为Android API 23上的CircleImageView。我从设备的内存中获取图像路径,并将此字符串路径存储到数据库中:

<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"
/>

它可以在API 19及更高版本上运行,但不能在具有Android版本API 23的Genymotion中的虚拟设备上运行。所选图像未显示在ImageView中。 我通过以下方式将图像设置为ImageView:

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

图像路径是mCurrentImagePath: /storage/emulated/0/Download/56836_(5-27-2006 2-28-40)(1920x1200).JPG

您知道为什么这行不通吗?

您使用FileUtils.getResizedBitmap()的方法存在问题。 我创建了一个示例项目来重现您的问题。 在该示例项目中运行良好。 使用以下代码段时,从设备的内存中加载图像不会有问题。

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;
}
  相关解决方案