2013-03-03 63 views
9

我有543 * 6423分辨率的图像,我想将其显示在所有设备中。这是显示在少数的Android手机,允许高分辨率。我试过位图太大而无法上传到某些手机的纹理中

android:hardwareAccelerated="false" 

这是我的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

参见[这](http://stackoverflow.com/a/15136744/1289716)的答案,希望它能帮助你。 – MAC 2013-03-03 05:40:52

+0

http://stackoverflow.com/questions/22633638/prevent-bitmap-too-large-to-be-uploaded-into-a-texture-android/24123605#24123605 看看我的答案 – Gilbert92 2014-06-09 15:43:43

回答

4

您的图片可能是过大的大多数设备显示。所以你必须加载缩小版本的图像。看看Loading Large Bitmaps Efficiently看看如何做到这一点,并计算出合适的样本量。 如果您需要显示宽度/高度(如果是全屏图像),您可以使用getResources().getDisplayMetrics()

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个值的权力。

3

在几个小时来编写和调试所有这些代码采样尝试手动与

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

,而不是花费数小时,为什么不尝试用毕加索?它是一个流行的图像加载库,用于处理各种类型和/或大小的位图。 我已经使用了这一行代码删除我的“位图太大......”问题:

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