【问题标题】:Access to sdcard image访问 sdcard 图像
【发布时间】:2011-10-20 05:56:59
【问题描述】:

我正在开发一个 android 应用程序,我必须在其中将图像从 sdcard 上传到互联网。为此,我使用 MediaStore 内容提供程序并从 MediaStore.Images.Media.DATA 列获取图像路径,现在我正在做的是创建一个 位图 从该路径使用 Bitmap.decodeFile(String path) 方法,然后在 List 中的 ImageView 中显示该位图。我面临的问题是,当我尝试转到在列表中显示此图像的活动时,我收到这样的错误 10-20 11:10:47.070: ERROR/AndroidRuntime(922): java.lang .OutOfMemoryError: 位图大小超出 VM 预算

任何人都可以建议我解决这个问题或实现我的目标的替代方法。提前致谢。

【问题讨论】:

  • 在设置图片为imageview之前最好使用图片代码的resize
  • 你的意思是我无法得到你。请对此评论进行更多说明。
  • 调整所选图像的大小。当您调整图像大小时,它会减小大小以避免内存不足异常
  • 您的位图使用了太多内存,因此请使用 OOM 在 google 上调整其大小以进行搜索,您将获得数千个帖子 stackoverflow.com/questions/6213690/…
  • 图片文件大小是多少?

标签: android bitmap sd-card


【解决方案1】:

这种情况经常发生,因为存储在 sdcard 中的图像具有高分辨率,并且您的应用无法将图像大小处理为堆大小

使用下面的行获取位图后

Bitmap bmp = Bitmap.decodeFile(String path) 

你应该尝试缩放到你需要的大小

newBmp = Bitmap.createScaledBitmap(bmp, 240, 320, true);

然后使用newBmp 在你的视图中设置。

【讨论】:

    【解决方案2】:

    以下代码用于调整所选图像的大小

    Bitmap bitmap  = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                int newWidth = 100;
                int newHeight = 100;
    
                // calculate the scale - in this case = 0.4f
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;
    
                // createa matrix for the manipulation
                Matrix matrix = new Matrix();
                // resize the bit map
                matrix.postScale(scaleWidth, scaleHeight);
    
                Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
                        width, height, matrix, true); 
    
                ((ImageView) findViewById(R.id.ImageviewTest)).setImageBitmap(resizedBitmap);
    

    【讨论】:

      【解决方案3】:
      String filepath = Environment.getExternalStorageDirectory()
                  .getAbsolutePath();
          File file = new File(filepath, filename);
          FileInputStream fs;
          try {
              fs = new FileInputStream(file);
              BitmapFactory.Options bfOptions = new BitmapFactory.Options();
              /*
               * bfOptions.inDither=false; //Disable Dithering mode
               * bfOptions.inPurgeable=true; //Tell to gc that whether it needs
               * free memory, the Bitmap can be cleared
               * bfOptions.inInputShareable=true;
               */
              bfOptions.inJustDecodeBounds = false;
              bfOptions.inTempStorage = new byte[32 * 1024];
              Bitmap b = BitmapFactory.decodeFileDescriptor(fs.getFD(), null,
                      bfOptions);
              img.setImageBitmap(b);
      }
      catch()
      {
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-11
        • 2017-05-22
        • 2013-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多