简体   繁体   English

位图无法加载2MB的图像

[英]Bitmap can't load a 2MB image

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. 如您所见,我使用选项调整了图像的大小,但是无论如何,它仅在最后一行崩溃,并抛出NullPointerException。 So imatgeOriginal is null 因此imatgeOriginalnull

Why is this happening? 为什么会这样呢?

Some notes: 一些注意事项:

  • The image is 2 MB (less than the 16 MB limit) and the size is 1105 × 1491 图像为2 MB(小于16 MB的限制),大小为1105×1491
  • The image does exist 图片确实存在
  • The same code works with images smaller than 1.5 MB 相同的代码适用于小于1.5 MB的图像

Output in logcat: 在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). 但是,Android会将此图像检测为CORRUPT或至少无法使用任何可用的工具打开(默认库,文件资源管理器,没有我的应用)。 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. 该应用程序也可以在iOS中运行,并且同一张图片实际上在那儿可见并且看起来没有损坏。

So I guess there's some limitations in Android to load JPEG files. 因此,我猜想Android在加载JPEG文件方面存在一些限制。

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. 我发现了一些资源: http : //code.google.com/p/skia/issues/detail? id=69#c2无法使用BitmapFactory.decodeFile加载JPEG图像。 Returns null 返回null

You can compress your bitmap size Bitmap.compress() 您可以压缩位图大小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 并尝试此方法,它将减小大小,这可能会帮助您加载它,因为对于某些手机来说2MB的空间很大

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");
}

Well I found out what is happening. 好吧,我发现了正在发生的事情。

There's an issue with Android and JPEG & CMYK pictures. Android和JPEG&CMYK图片存在问题。

You can read more: http://code.google.com/p/skia/issues/detail?id=69#c2 您可以阅读更多信息: http : //code.google.com/p/skia/issues/detail?id=69#c2

Unable to load JPEG-image with BitmapFactory.decodeFile. 无法使用BitmapFactory.decodeFile加载JPEG图像。 Returns null 返回null

This error is known since 2.0 and there's no workaround at the moment. 从2.0开始就知道该错误,目前尚无解决方法。 All you can do is being sure that you won't download any image that have the two properties mentioned above. 您所要做的就是确保您不会下载具有上述两个属性的任何图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM