Loading drawable from sd card

后端 未结 2 565
忘了有多久
忘了有多久 2020-12-17 02:32

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()
{
retu         


        
相关标签:
2条回答
  • 2020-12-17 02:44

    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);
    }
    
    0 讨论(0)
  • 2020-12-17 02:47

    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.

    0 讨论(0)
提交回复
热议问题