某些手机中,位图太大无法上传到纹理中。

11
我有一张分辨率为543*6423的图片,希望在所有设备上都能显示。它能在一些允许高分辨率的安卓手机上显示。我尝试了以下方法:
android:hardwareAccelerated="false"

这是我的 android 客户端的 Java 代码

File storagePath =Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS+
                    "abc.png");
            InputStream is = this.getContentResolver().openInputStream(Uri.fromFile(storagePath)); 
            Bitmap b = BitmapFactory.decodeStream(is, null, null); 
            is.close(); 
            image.setImageBitmap(b);

这在我的手机(索尼Xperia)上可行,但其他一些手机无法显示。请帮我解决如何独立于屏幕分辨率显示此图像的问题。
谢谢, 阿曼

1
请查看此答案,希望能对您有所帮助。 - MAC
请查看我的回答:https://dev59.com/_mEh5IYBdhLWcg3wPhZT#24123605 - Gilbert92
5个回答

4

尝试使用

android:hardwareAccelerated="false" 
android:largeHeap="true"

4

您的图片可能太大,无法在大多数设备上显示。因此,您需要加载缩小版本的图像。请参阅高效加载大型位图以了解如何执行此操作并计算适当的采样大小。 如果您需要显示宽度/高度(在全屏图像的情况下),可以使用getResources().getDisplayMetrics()


0

与其花费数小时手动编写和调试所有这些降采样代码,为什么不尝试使用Picasso呢?它是一款流行的图像加载库,专门用于处理各种类型和/或大小的位图。 我使用了这一行代码来解决我的“位图太大…”问题:

Picasso.with(image.getContext()).load(storagePath).fit().centerCrop().into(image);

0

博客中的解决方案错了一个2的幂次方...

这里是正确的解决方案:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

// Set height and width in options, does not return an image and no resource taken
BitmapFactory.decodeStream(imagefile, null, options);

int pow = 0;
while (options.outHeight >> pow > reqHeight || options.outWidth >> pow > reqWidth)
    pow += 1;
options.inSampleSize = 1 << pow; 
options.inJustDecodeBounds = false;
image = BitmapFactory.decodeStream(imagefile, null, options);

图片将按照reqHeight和reqWidth的尺寸缩小。我理解inSampleSize只接受2的幂值。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接