BitmapFactory.decodeStream (fileInputStream) returns null - Android

5

Good!

I have a problem with the following code in Android Studio:

try {
 FileInputStream fileInputStream = new FileInputStream(dirPhoto);
 bitmap = BitmapFactory.decodeStream(fileInputStream);
 vwImagen.setImageBitmap(bitmap);
  } 
catch (SecurityException io){
} 
 catch (FileNotFoundException io) {
 }

The fileInputStream file seems to be created correctly, but when wanting to convert it to Bitmap, the function BitmapFactory.decodeStream(fileInputStream) returns a null value.

It is worth clarifying that it is an image in jpg. The value of dirPhoto is:

/data/data/com.example.leandro.application/files/pic_20170316104409.jpg

Thank you!

    
asked by ZottoSL 17.03.2017 в 19:37
source

2 answers

1

If you have the file path, you can use decodeFile ()

try {
 //FileInputStream fileInputStream = new FileInputStream(dirPhoto);
 //bitmap = BitmapFactory.decodeStream(fileInputStream);
  bitmap = BitmapFactory.decodeFile(dirPhoto);
 vwImagen.setImageBitmap(bitmap);
  }catch (SecurityException io){
  }catch (FileNotFoundException io) {
 }
  

decodeFile () Decode a file path in a bitmap. If he   The specified file name is null or can not be decoded in a   bitmap, the function returns null.

    
answered by 18.03.2017 / 00:57
source
0

I recommend declaring it this way:

bitmap = BitmapFactory.decodeFile (PhotoDir);

vwImagen.setImageBitmap (bitmap);

    
answered by 18.03.2017 в 00:50