Maycon Fábio de Oliveira
Lurker
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!
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!