Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading drawable from sd card

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);
}

The image path is: 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?

like image 876
idish Avatar asked Sep 29 '12 12:09

idish


1 Answers

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:

The bitmap can be created with 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

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

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

like image 88
IAmGroot Avatar answered Sep 19 '22 02:09

IAmGroot