从SD卡装载绘制SD

2023-09-12 10:21:34 作者:吃人家一锤嘛

我试图加载一个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);

看看Here (位图)和Here (位图可绘制)以获得更多信息。

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

 
精彩推荐
图片推荐