• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps BitmapFactory.decodeByteArray always null (what is wrong?)

Hi there.

The code below is a little bit messy because I already tried all possible solutions to show an image on ImageView.

The ideia is to load a image from internal memory and show on ImageView

The problem is that BitmapFactory.decodeByteArray is always returning null for the image loaded

OBS: R.raw.teste is a valid image

OBS2: The for block is there only because the code was intended to fill a ListView.



try {
InputStream inStream = getApplicationContext().getResources().openRawResource(
R.raw.teste);
// byte[] music = new byte[inStream.available()];
FileOutputStream fos = openFileOutput("teste.png", MODE_PRIVATE);

byte[] buffer = new byte[1024];
int length = 0;
try {
while ((length = inStream.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
System.out.println(length);
} catch (IOException ioe) {
/* ignore */
}

inStream.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}


String[] files = getApplicationContext().fileList();
for (String file : files) {
if (file.endsWith("png")) {
try {

FileInputStream fileInputStream = openFileInput(file);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while (fileInputStream.available() > 0) {
bos.write(fileInputStream.read());
}

byte[] bytes = bos.toByteArray();

imgview.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));

System.out.println(bytes.length);
} catch (Exception e) {
e.printStackTrace();
}
}
}


Any ideias?

Thanks!
 
Hi!

On my OS I can view de image normally. (how to examine the image saved on internal memory?)
The file is always the same (I´m just testing one file).
I also checked the image bytes size after and before persistence and load. Is the same.
 
Back
Top Bottom