【问题标题】:Loading big image to bitmap in android在android中将大图像加载到位图
【发布时间】:2013-07-21 03:42:59
【问题描述】:

您好,我正在使用以下代码从 sdcard 加载图像,它运行正常,

Bitmap picture=BitmapFactory.decodeFile("/sdcard...");

或

Bitmap picture= BitmapFactory.decodeByteArray(byte[]..);  

byte[] 数组包含使用FileInputstream 从 sdcard 读取的字节,并且不为空。以上两个代码都可以正常工作。问题是它们不适用于较大的图像,例如我有一个 1.8 mb 大小的图像。我的应用程序在解码图像时崩溃。用于大图像的任何方法都失败了。 任何解决方案请谢谢。

【问题讨论】:

  • 调整图像大小然后使用它。
  • 这可能是什么原因,以便可以解决,因为我需要使用原始图像。
  • 由于android中堆的大小,而你的位图在堆中分配内存。
  • 现在我已经调试了它,当调试器到达上面提到的命令时它会停止并说源丢失。
  • 这是这个问题的重复:stackoverflow.com/questions/2798756/…

标签: android image


【解决方案1】:

尝试创建可清除的位图。

   byte[] data = ...(read byte array from file)
   BitmapFactory.Options opt = new BitmapFactory.Options();
   opt.inPurgeable = true;
   Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length, opt); 

【讨论】:

    【解决方案2】:

    使用以下代码将图像调整为您需要的任何大小..

     Bitmap picture=BitmapFactory.decodeFile("/sdcard...");
        int width = picture.getWidth();
        int height = picture.getWidth();
        float aspectRatio = (float) width / (float) height;
        int newWidth = 70;
        int newHeight = (int) (70 / aspectRatio);       
        picture= Bitmap.createScaledBitmap(picture, newWidth, newHeight, true);
    

    【讨论】:

    • 您还必须将图像加载到内存中,这样不能解决问题。
    • 问题是应用程序在此命令上崩溃...Bitmap picture=BitmapFactory.decodeFile("/sdcard...");
    • 经过大量工作后,我发现问题不在于代码,而在于模拟器的 ram 大小,编辑 avd 和增加 ram 大小可以解决所有问题并轻松保存巨大的图片。谢谢。
    【解决方案3】:

    http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 上的教程解释了如何在不遇到可怕的 OutOfMemoryException 的情况下将大图像加载到位图中。

    【讨论】:

      【解决方案4】:

      Android VM 有内存限制,这限制了可解码图像的大小。要在图像视图中显示调整后的图像,可以使用以下代码。

      decode_options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(temp,decode_options);  //This will just fill the output parameters
      if(decode_options.outWidth > image_width
              || decode_options.outHeight > image_height)
      {
          float scale_width,scale_height;
      
          scale_width = ((float)decode_options.outWidth) / image_width;
          scale_param = scale_width;
          scale_height = ((float)decode_options.outHeight) / image_height;
      
          if(scale_param < scale_height)
              scale_param = scale_height;
      }
      
      decode_options.inJustDecodeBounds = false;
      decode_options.inSampleSize  = (int)(scale_param + 1);
      decode_options.inPreferredConfig =  Bitmap.Config.ARGB_8888;
      decoded_data = 
              BitmapFactory.decodeFile(temp,decode_options);
      

      【讨论】:

        猜你喜欢
        • 2011-02-17
        • 1970-01-01
        • 2016-06-06
        • 1970-01-01
        • 1970-01-01
        • 2013-04-29
        • 2012-03-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多