从 SD 卡加载可绘制 [英] Loading drawable from sd card

查看:24
本文介绍了从 SD 卡加载可绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的设备 SD 卡加载一个 png 图像作为可绘制对象.我使用以下功能,但它不起作用:

I'm trying to load a png image as a drawable from my device sd card. I use the following function but it doesn't work:

public Drawable getDrawable()
{
return new BitmapDrawable(imagePath);
}

图片路径为:mnt/sdcard/MyFolder/image.png

The image path is: mnt/sdcard/MyFolder/image.png

当我尝试调用该方法时应用程序崩溃,我应该如何加载位于我的 sdcard 中的 png 图像并将其转换为 Drawable 对象?

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?

推荐答案

实际上有一个直接来自文件路径的 BitmapDrawable 构造函数.您使用的方法已弃用.试试:

There is actually a BitmapDrawable constructor straight from file path. 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:

位图可以用 decodeFile

你可以这样使用它:

Bitmap myBitmap = BitmapFactory.decodeFile(pathName);

然后你可以使用位图进行绘图等

Then you can use the bitmap for drawing etc.

将Bitmap转换为drawable使用

to convert Bitmap to drawable use

Drawable myDrawable = new BitmapDrawable(getResources(), myBitmap);

看看这里(位图)此处(位图可绘制对象)了解更多信息.

Take a look Here (Bitmaps) and Here (Bitmap Drawables) for more info.

这篇关于从 SD 卡加载可绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆