Android: how to create a BitmapDrawable from a file in the file system?

192 Views Asked by At

So I'm trying to restore an image from a file in my app's private files directory.

InputStream inputStream;
BitMapDrawable result;
    try {
        inputStream = new FileInputStream(file.getAbsolutePath());
        result = BitmapDrawable.createFromStream(inputStream, null);
        inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } 
return result;

But obviously I'm doing something wrong because result is always null.

2

There are 2 best solutions below

1
On
new BitmapDrawable( BitmapFactory.decodeFile(file.getAbsolutePath()) );
1
On

Isn't It what you want?

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
Drawable d = new BitmapDrawable(getResources(),bitmap);
imageview.setImageDrawable(d);