【问题标题】:How to display large size bitmaps in imageview android?如何在imageview android中显示大尺寸位图?
【发布时间】:2014-07-17 05:49:42
【问题描述】:

我的图片高度超过 6000 像素。每当我尝试显示这些图像时,都会出现内存不足异常。

我看过很多链接,但没有一个符合我的需要。因为大多数人建议图像调整大小的解决方案。但是如果我重新调整图片的大小,由于质量差,我无法阅读图片中的文字。

我需要一些帮助来创建一些可以打开图像而无需重新调整大小的代码,也可以使用缩放效果。

任何帮助将不胜感激。

【问题讨论】:

    标签: android bitmap imageview out-of-memory


    【解决方案1】:

    尝试使用google 推荐解决此问题。

    为避免 OOM,您需要实现此代码块:

    public static int calculateInSampleSize(
                BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
    
        if (height > reqHeight || width > reqWidth) {
    
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
    
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
    
        return inSampleSize;
     }
    

    【讨论】:

    • 在某些情况和某些设备上,您需要重新调整图像大小,因为设备上的 OPENGL ES 对渲染图像大小有一些限制
    【解决方案2】:

    尝试使用 WebView 来显示图像,而不是 ImageView

    【讨论】:

      【解决方案3】:

      我建议您将此图像“剪切”为 3 个较小的图像,然后将它们加载为三个图像:第一个将在 y:0 处,高度为 2000px,第二个为 y:2000px,高度为 2000px,第三个一个在y:4000px。 你认为这可行吗? 祝你好运

      【讨论】:

      • 我不认为它会起作用......因为我还需要实现缩放效果......并且用户必须多次点击才能缩放每个图像......
      • 您可以将三个图像插入一个容器中,然后缩放整个容器,这样您将获得与缩放整个图像相同的效果。
      【解决方案4】:

      试试这个:

       @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_fullscreen_view);
      
          imageView = (ImageView) findViewById(R.id.full_image_view);
          imageView.setImageBitmap(decodeSampledBitmapFromResource(imgpath));
      }
      
      
        //Load a bitmap from a resource with a target size
      Bitmap decodeSampledBitmapFromResource(String res) {
          // First decode with inJustDecodeBounds=true to check dimensions
          final BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(res, options);
      
          //Calculate display dimention for maximum reqwidth and reqheigth 
          Display display = getWindowManager().getDefaultDisplay();
          Point size = new Point();
          display.getSize(size);
          int xDim = size.x;
          int yDim = size.y;
      
      
          // Calculate inSampleSize
          options.inSampleSize = calculateInSampleSize(options, xDim, yDim);
          // Decode bitmap with inSampleSize se5t
          options.inJustDecodeBounds = false;
          return BitmapFactory.decodeFile(res, options);
      }
      
      
      int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
          int inSampleSize = 1; //Default subsampling size
          // Calculate the largest inSampleSize value 
          while ((options.outHeight / inSampleSize) > reqHeight
                  || (options.outWidth / inSampleSize) > reqWidth) {
              inSampleSize += 1;
          }
          return inSampleSize;
      }
      

      【讨论】:

        【解决方案5】:

        在应用程序清单文件标记设置 largeHeap true

         <application
                 android:largeHeap="true"
        

        也可以使用 Picasso 或 Glide 在 ImageView 中加载图像

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-10
          • 2022-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多