[英]Loading drawable from sd card
I'm trying to load a png image as a drawable from my device sd card. 我正在尝试从我的设备SD卡中加载一个png图像作为drawable。 I use the following function but it doesn't work: 我使用以下函数,但它不起作用:
public Drawable getDrawable()
{
return new BitmapDrawable(imagePath);
}
The image path is: mnt/sdcard/MyFolder/image.png 图像路径为:mnt / sdcard / MyFolder / image.png
The app crashes when I try calling that method, how should I load my png image located in my sdcard and cast it into a Drawable object? 当我尝试调用该方法时应用程序崩溃,我应该如何加载位于我的SD卡中的png图像并将其转换为Drawable对象?
There is actually a BitmapDrawable constructor straight from file path. 实际上直接来自文件路径的BitmapDrawable构造函数。 The method you are using is depricated. 您正在使用的方法已被删除。 Try: 尝试:
Drawable myDrawable = new BitmapDrawable(getResources(), pathName);
If this doesnt work, Try getting a bitmap and creating a drawable from it: 如果这不起作用,请尝试获取位图并从中创建一个drawable:
The bitmap can be created with decodeFile
可以使用decodeFile
创建位图
You can use it like this: 你可以像这样使用它:
Bitmap myBitmap = BitmapFactory.decodeFile(pathName);
Then you can use the bitmap for drawing etc. 然后你可以使用位图进行绘图等。
to convert Bitmap to drawable use 将Bitmap转换为可绘制用途
Drawable myDrawable = new BitmapDrawable(getResources(), myBitmap);
Take a look Here (Bitmaps) and Here (Bitmap Drawables) for more info. 有关详细信息,请查看此处(位图)和此处(位图Drawables) 。
I am simple do like that 我这么简单
public Drawable getDrawableFromPath(String filePath) {
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
//Here you can make logic for decode bitmap for ignore oom error.
return new BitmapDrawable(bitmap);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.