【发布时间】:2025-12-17 11:10:01
【问题描述】:
我的可绘制文件夹中已经有一些来自 android 项目的图片。
我创建了一些对象(代理),然后我需要使用我保存在数据库中的这张图片来设置imageView。
所以,我将图片保存为String photoPath:
Uri path1 = Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" + R.drawable.agent1);
String photoAg1 = path1.getPath();
ag1.setPhotoPath(photoAg1);
(我已经尝试过 path1.toString。)
好的,在此之前没有问题。
此对象显示在 listView 和 ImageView。
为了展示它,我正在做:
ImageView photo = (ImageView) view.findViewById(R.id.list_cell_photo);
System.out.println(agent.getPhotoPath());
Bitmap bitmap = BitmapFactory.decodeFile(agent.getPhotoPath());
Bitmap lowdefbitmap = Bitmap.createScaledBitmap(bitmap,300,300,true);
photo.setImageBitmap(lowdefbitmap);
问题出在createScaledBitmap。
上面的错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:714)
从System.out.println返回:/2131099732
如果我输入path1.toString,则返回:android.resource://com.aa.xlambton/2131099732
我已经看过了: Create a Bitmap/Drawable from file path
Android get image path from drawable as string
Get absolute path of android drawable image
我认为我保存此路径错误,因为Bitmap 无法解码文件,因此createdScaledBitmap 将变为空。
有人可以帮帮我吗?
【问题讨论】:
-
你想将drawable转换为位图吗?你为什么需要它?只需简单地将可绘制对象设置为图像资源
-
这个link的可能重复
标签: android image bitmap path android-drawable