8

I am getting Image as BLOB type from API. And, I am saving as follows,

public static void setOptimalImage(Context ctx, File file, ImageView iv,
            Point size) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getPath(), options);
        if (options.outHeight > 0 && options.outWidth > 0) {
            if (size == null) {
                size = new Point();
                WindowManager wm = (WindowManager) ctx
                        .getSystemService(Context.WINDOW_SERVICE);
                wm.getDefaultDisplay().getSize(size);
            }
            options.inSampleSize = Utils.calculateInSampleSize(options, size.x,
                    size.y);
            options.inJustDecodeBounds = false;
            try {
                iv.setImageBitmap(BitmapFactory.decodeFile(file.getPath(),
                        options));
            } catch (OutOfMemoryError oome) {
                Log.e(Utils.class.getName(),
                        oome.getMessage() == null ? "OutOfMemory error: "
                                + file.getName() : oome.getMessage());
            }
        }
    }

Now, the issue i am facing is,

When I am saving it, sometimes the image is saving only half.Now, my question is,

  • Is there any way to check the image from the file path is half loaded (Which means corrupted) ?

Edit:

  • I am downloading an image from server. In the mean while, i have lost my net connection. In that case, my image is downloading half. Can i get those corrupted images programmatically ? Or Any idea how to handle in this Case ?

Any help would be really appreciated.

Thanks.

Attaching Image for Reference:

enter image description here

Manu
  • 4,730
  • 2
  • 20
  • 45
  • @Manu You need to refresh the gallery after saving the image http://stackoverflow.com/questions/15837485/how-can-i-update-the-android-gallery-after-a-photo and after refreshing you need to set the image – KK_07k11A0585 Jun 16 '15 at 05:18

2 Answers2

1

I think you look into incorrect direction of detecting corrupted images. It would be easy to manage loaded (or semi-loaded) bitmaps using API and requests. You can also have a file (or even better a database), in which you will have an information about downloaded images.

I see multiple checks, "is image downloaded?":

  • API should tell you what is expected size of image you want to download. After loading is finished you have to check sizes and compare are they equals
  • You can detect with what error code your request is finished. If it is finished with success code you can mark you image is loaded

And after during display you can detect which images are fully loaded.

Oleksandr
  • 6,226
  • 1
  • 46
  • 54
0

Ideally you should make the API return a checksum (e.g. MD5) with each file. You can then generate a checksum using the same algorithm in Android for the downloaded file and compare it with the one returned from the API. Look into the java.security.MessageDigest class for details on how to generate a checksum.

emidander
  • 2,383
  • 22
  • 29