Check image with half load or not in Android

I get the image as a blob type from the API. And I save the following:

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 problem I am facing is

When I save it, sometimes the image saves only half. Now my question is:

  • Is there a way to check the image from the file path half loaded (which means damage)?

Edit:

  • I am downloading an image from the server. At the same time, I lost my network connection. In this case, my image is half loaded. Can I get these damaged images programmatically? Or any idea how to handle in this case?

Any help would be really appreciated.

Thanks.

Attaching an image for reference:

enter image description here

+6
source share
2 answers

I think you are looking at the wrong direction to detect damaged images. It would be easy to manage uploaded (or semi-loaded) bitmaps using APIs and queries. You may also have a file (or even a better database) in which you will have information about the downloaded images.

I see several checks, "is the image uploaded?" :

  • The API should tell you that the size of the image you want to download is expected. After the download is complete, you need to check the sizes and compare them equal
  • You can determine with what error code your request is completed. If the code is completed with a success code, you can note that the image has been uploaded.

And after displaying you can find out which images are fully loaded.

0
source

Ideally, you should force the API to return a checksum (e.g. MD5) with each file. Then you can create a checksum using the same algorithm in Android for the downloaded file and compare it with the one that was returned from the API. Examine the java.security.MessageDigest class to learn how to generate a checksum.

0
source

Source: https://habr.com/ru/post/989172/


All Articles