Failed to load JPEG image using BitmapFactory.decodeFile. Returns null

I am creating an application that displays many images created in Imagemagick PDF files. Some images cannot be uploaded using BitmapFactory. It just returns the null istead bitmap.

The magazine says:

D/skia(15101): --- decoder->decode returned false 

This is not a memory problem, as some of the images with the problem are very small, and the images are not damaged, because I can display them on any other machine. In addition, BitmapFactory can decode width and height if I use

  inJustDecodeBounds = true; 

in the parameters.

I tried to download one of the images using an external image viewer ( QuickPic ) with no luck. It also returns a “Download Error”, indicating that SKIA believes the image is corrupt or at least not supported for any reason.

One of the images that do not work can be found here.

The full code that I use to download it is here

  BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeFile(FILENAME,o); int width = o.outWidth; int height = o.outHeight; /* Width and height decoded successfuly */ BitmapFactory.Options o2 = new BitmapFactory.Options(); o.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(FILENAME,o2); /*Bitmap is null */ 

Any idea of ​​what's wrong or how to get around it is welcome.

+1
source share
2 answers

Apparently SKIA has a problem with JPG with a CMYK profile. The workaround for my problem was to add the "-colorspace RGB" option to my imagemagick conversion.

+1
source

Images with a CMYK color space, such as this , cannot be decoded by Android by default so that they are not displayed.

Niels was right, but if you cannot change this parameter when converting imageMagick, you can convert CMYK image to RGB image (supported by Android). There is an imageMagick library for Android called android-lib-magick and here is a workaround to this problem.

Just import this library. The code for converting your color space is pretty simple:

 ImageInfo info = new ImageInfo(path); // path where the CMYK image is your device MagickImage imageCMYK = new MagickImage(info); imageCMYK.transformRgbImage(ColorspaceType.CMYKColorspace); Bitmap bitmap = MagickBitmap.ToBitmap(imageCMYK); 

If the image is not on your device or on your SD card, you need to download it first.

I implemented two simple methods using android-lib-magick called "getCMYKImageFromPath" and "getCMYKImageFromURL". You can see the code here :

https://github.com/Mariovc/GetCMYKImage

+1
source

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


All Articles