As part of my program, I need to show images belonging to a folder (JPG files). To accomplish that, I have this code:
private ArrayList<ImageGalleryItem> getData() {
final ArrayList<ImageGalleryItem> imageItems = new ArrayList<>();
try {
Intent intent = getIntent();
String imagesFolder = intent.getStringExtra("SourceFolder");
String questionId = Integer.toString(intent.getIntExtra("QuestionId", 0));
File file = new File(imagesFolder);
if (file.exists() && file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
try {
if (f.getName().startsWith(questionId + "_")) {
Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
imageItems.add(new ImageGalleryItem(bitmap, f.getName()));
}
}
catch (Exception e)
{
Log.e("TDC@", e.getMessage());
}
}
}
}
catch (Exception e)
{
Log.e("TDC@", e.getMessage());
}
return imageItems;
}
The instruction "BitmapFactory.decodeFile(f.getPath());" creates the bitmap files for all files but for one of them. When that code is reached for the conflicting file, decodeFile throws an exception, but the stranger thing is that the exception is not catched by the try catch block.
When debugging using F7, the exception is thrown in BitmapFactory.java, in the throw shown in this code:
try {
bm = nativeDecodeByteArray(data, offset, length, opts);
if (bm == null && opts != null && opts.inBitmap != null) {
throw new IllegalArgumentException("Problem decoding into existing bitmap");
}
setDensityFromOptions(bm, opts);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
}
So the questions are, why the bitmap cannot be correctly decoded and why the exception is not catched.
If I browse for the file in mobile file manager, and open the file, it is shown correctly, so, there is no problem with the image format. Furthermore, this image was taken with the camera, the same as the other images that do decode correctly.
How to solve this problem or, is there an alternate way to do this?