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

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

问题描述

我试图加载一个PNG图像作为我的设备的SD卡中的绘制。 我用下面的功能,但它不工作:

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 / SD卡/ MyFolder中/ image.png

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

应用程序崩溃,当我尝试调用该方法,我应该怎么加载我的PNG图像位于我的SD卡丢在一个可绘制对象?

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构造直接从文件的路径。您所使用的方法是depricated。尝试:

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:

位图可与创建德codeFILE

您可以使用这样的:

Bitmap myBitmap = BitmapFactory.decodeFile(pathName);

然后你可以使用位图绘制等。

Then you can use the bitmap for drawing etc.

为位图转换为可绘制的使用

to convert Bitmap to drawable use

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

看看<一href="http://developer.android.com/reference/android/graphics/BitmapFactory.html#de$c$cFile%28java.lang.String">Here (位图)和<一href="http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html#BitmapDrawable%28android.content.res.Resources,%20android.graphics.Bitmap%29">Here (位图可绘制)以获得更多信息。

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

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

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