0

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?

jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • If the exception is already being caught it can't be caught in your code. The first catch catches it: https://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html Anyway, the android code is already dealing with the error and that error is relating to "Problem decoding into existing bitmap". So your providing an illegal argument. Somehow that file is different. Remove the offending image and see if it completes. If it completes, look at the offending image and see what's different about it, re-save it, etc. – CmosBattery Oct 23 '15 at 02:08

1 Answers1

0

I don't know why that happens. Try this:

Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f));

  • It does not work either, however, I have deletedthe offending image and the problem is not repeated – jstuardo Oct 23 '15 at 14:54