大图像问题(java.lang.OutOfMemoryError:位图的大小超过VM预算) [英] Problem with big images ( java.lang.OutOfMemoryError: bitmap size exceeds VM budget )

查看:120
本文介绍了大图像问题(java.lang.OutOfMemoryError:位图的大小超过VM预算)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个很多人我有很大的映像有问题了。
好吧,我在这里的code。我的计算器如何使用BitmapFactory.Options解决这一问题找到。但在我的情况下,我得到的图像不是从文件。谁能告诉我如何与已在内存中创建(我从相机上的电话本图片)

as a many people i have a problem with big images too. ok,here my code. I found on stackoverflow how to fix this using BitmapFactory.Options. But in my situations i get image not from file. Can anyone tell me how to do this with Bitmap which already created in memory ( i get this picture from camera on phone)

 public void getImageAndSend( )
  {

     Bitmap newbmp ;
         newbmp= Bitmap.createBitmap(oBmp.getWidth(), oBmp.getHeight(),oBmp.getConfig());////<----------error here
      log("widt oBmp = "+oBmp.getWidth());/// width = 2048
      log(" height oBmp = "+oBmp.getHeight());///height 1538
      //BitmapFactory.Options options = new BitmapFactory.Options(); // <----- it's possible to do it without `BitmapFactory.decodeFile(pathToFile,options) ?? 
      //options.inTempStorage = new byte[16*1024];
          Canvas canvas = new Canvas(newbmp);
          //code 
  System.gc();/// can't right understand but this line fixed this problem on emulator but not on real device   

}

请告诉我如何解决我的问题? :(
在模拟器我不是有这样的错误。但在实际的设​​备,我看到这个错误(位图大小超过VM预算)
问候,彼得。

please tell me how to fix my problem ? : ( On emulator im not have this error. But on real device i see this error (bitmap size exceeds VM budget) Regards, Peter.

推荐答案

既然你都可以从摄像机图像,获得图像您在捕捉的onActivityResult()方法如下URI

since you are getting image from camera,get the uri of the image you capture in onActivityResult() method as follows

Uri selectedImageUri=intent.getData();
String actualPath=getRealPathFromURI(selectedImageUri);
File file=new File(actualPath);
Bitmap bitmap=decodeFile(file); //this is new bitmap which you can use for your purpose 

public String getRealPathFromURI(Uri contentUri) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }


private Bitmap decodeFile(File f){
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //The new size we want to scale to
            final int REQUIRED_SIZE=70;

            //Find the correct scale value. It should be the power of 2.
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

希望这helps.This是很熟悉的问题时ü应对来自phone.check <一所高质量的图像href=\"http://stackoverflow.com/questions/3956702/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget\">this出过

这篇关于大图像问题(java.lang.OutOfMemoryError:位图的大小超过VM预算)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆