1

This is the code I'm using:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap imatgeOriginal = BitmapFactory.decodeFile(imageUrl, options);
Bitmap resizedbitmap = Bitmap.createScaledBitmap(imatgeOriginal, 1000, 1000, true);

As you can see I resize the image with the Options, but anyways it just crashes on the last line, throwing a NullPointerException. So imatgeOriginal is null

Why is this happening?

Some notes:

  • The image is 2 MB (less than the 16 MB limit) and the size is 1105 × 1491
  • The image does exist
  • The same code works with images smaller than 1.5 MB

Output in logcat:

08-22 15:11:13.249: E/AndroidRuntime(5404): FATAL EXCEPTION: main
08-22 15:11:13.249: E/AndroidRuntime(5404):
java.lang.NullPointerException 08-22 15:11:13.249:
E/AndroidRuntime(5404):     at
android.graphics.Bitmap.createScaledBitmap(Bitmap.java:432) 08-22
15:11:13.249: E/AndroidRuntime(5404):   at
myclass.onAnimationEnd(productView.java:2126) 08-22 15:11:13.249:
E/AndroidRuntime(5404):     at
android.view.animation.AnimationSet.getTransformation(AnimationSet.java:397)

Edit:

Something weird is happening, the File Image exists, the route is correct. However android detects this image as CORRUPT or atleast it can't be opened with any tool available (Default gallery, file explorer, no my app). However, if I download the image to my computer, the image is displayed perfectly. This app is also run in iOS and the same image is actually visible there and looking not corrupt.

So I guess there's some limitations in Android to load JPEG files.

Did you guys have any problem similar like this?

Some resources I've found: http://code.google.com/p/skia/issues/detail?id=69#c2 Unable to load JPEG-image with BitmapFactory.decodeFile. Returns null

Community
  • 1
  • 1
Reinherd
  • 5,476
  • 7
  • 51
  • 88
  • Post the logcat output. – S.D. Aug 22 '13 at 13:13
  • 1
    Whoever voted to close this question shouldnt have done it. There's nothing similar on that question... omg. – Reinherd Aug 22 '13 at 13:14
  • @S.D. Done, check it out. the line 2126 is exactly the last one I've put in this post. – Reinherd Aug 22 '13 at 13:17
  • 1
    For some reason, imatgeOriginal is null. Are you sure your imageUrl is correct and decoding the bitmap correctly? – tim.paetz Aug 22 '13 at 13:18
  • In the logcat it should appear a message from the bitmapfactory explaining why it couldn't decode your image. – Juangcg Aug 22 '13 at 13:19
  • 1
    @SergiCastellsaguéMillán Looks like `imatgeOriginal` is coming null. Which means `decodeFile()` isn't doing its thing, is the image file present, and is it a correct image format ? – S.D. Aug 22 '13 at 13:23
  • @S.D. as you found out, the problem was the last thing you mentioned. It seems to be corrupt. However decodeFile method didn't show any helpful message. Thanks. – Reinherd Aug 22 '13 at 13:26
  • @SergiCastellsaguéMillán That's because native C code is used to decode, native code can only return a value. – S.D. Aug 22 '13 at 13:27
  • @S.D. I see. However, the issue is not solved. I edit my question to explain what is happening exactly. – Reinherd Aug 22 '13 at 13:36
  • @SergiCastellsaguéMillán If you have an image editor on PC, Open it there, and then export it to jpeg/png, copy to android, try to open. – S.D. Aug 22 '13 at 13:41
  • It works. The issue is that JPEG with CMYK crashes in Android. Check the two links I provided in my question, in my edit exactly. – Reinherd Aug 22 '13 at 13:45

2 Answers2

0

You can compress your bitmap size Bitmap.compress()

and try this method it'll reduce the size which will maybe help you loading it, because 2MB is huge for some phones

int MAX_IMAGE_SIZE = 200 * 1024; // max final file size
Bitmap bmpPic = BitmapFactory.decodeFile(fileUri.getPath());
if ((bmpPic.getWidth() >= 1024) && (bmpPic.getHeight() >= 1024)) {
    BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
    bmpOptions.inSampleSize = 1;
    while ((bmpPic.getWidth() >= 1024) && (bmpPic.getHeight() >= 1024)) {
        bmpOptions.inSampleSize++;
        bmpPic = BitmapFactory.decodeFile(fileUri.getPath(), bmpOptions);
    }
    Log.d(TAG, "Resize: " + bmpOptions.inSampleSize);
}
int compressQuality = 104; // quality decreasing by 5 every loop. (start from 99)
int streamLength = MAX_IMAGE_SIZE;
while (streamLength >= MAX_IMAGE_SIZE) {
    ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
    compressQuality -= 5;
    Log.d(TAG, "Quality: " + compressQuality);
    bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
    byte[] bmpPicByteArray = bmpStream.toByteArray();
    streamLength = bmpPicByteArray.length;
    Log.d(TAG, "Size: " + streamLength);
}
try {
    FileOutputStream bmpFile = new FileOutputStream(finalPath);
    bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpFile);
    bmpFile.flush();
    bmpFile.close();
} catch (Exception e) {
    Log.e(TAG, "Error on saving file");
}
Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
  • If I'm not wrong, you can't compress while loading. First you've to load, then compress. Anyways in my code I am already compressing the image, but it's crashing on loading it. – Reinherd Aug 22 '13 at 13:23
  • Oh, maybe the image is not loading to where it's suppose to x.x – Ahmed Ekri Aug 22 '13 at 13:26
0

Well I found out what is happening.

There's an issue with Android and JPEG & CMYK pictures.

You can read more: http://code.google.com/p/skia/issues/detail?id=69#c2

Unable to load JPEG-image with BitmapFactory.decodeFile. Returns null

This error is known since 2.0 and there's no workaround at the moment. All you can do is being sure that you won't download any image that have the two properties mentioned above.

Community
  • 1
  • 1
Reinherd
  • 5,476
  • 7
  • 51
  • 88