简体   繁体   English

java OutOfMemoryError:位图大小超出VM预算

[英]java OutOfMemoryError: bitmap size exceeds VM budget

I have ImageView & one button on my ImageActivity. 我的ImageActivity上有ImageView和一个按钮。 when the button is clicked the available images are listed, when one of them is clicked, it is loaded into the ImageView. 单击该按钮时,将列出可用的图像,当单击其中一个图像时,它将被加载到ImageView中。

imageView xml: imageView xml:

   <ImageView
        android:id="@+id/imageView"
        android:layout_width="300dip"
        android:layout_height="300dip"
        android:src="@android:drawable/alert_light_frame" 
   />

Please see the code: 请查看代码:

public class ImageActivity extends Activity {

    private static final int SELECT_PICTURE = 1; 
    private String selectedImagePath;
    private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.comments_detail);

    imageView = (ImageView) findViewById(R.id.imageView);

      ((Button) findViewById(R.id.BtnBrowse))
       .setOnClickListener(new OnClickListener() 
       { 
          public void onClick(View arg0) 
          { 
              Intent intent = new Intent(); 
              intent.setType("image/*"); 
              intent.setAction(Intent.ACTION_GET_CONTENT); 
              startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
         }
       });
}

    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
        if (resultCode == RESULT_OK) 
        {
             if (requestCode == SELECT_PICTURE) 
             { 
                Uri selectedImageUri = data.getData(); 
                selectedImagePath = getPath(selectedImageUri); 
                //System.out.println("Image Path : " + selectedImagePath);
                imageView.setImageURI(selectedImageUri);  
           }  
       } 
    } 

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


}

Issue: The issue is that when i click the button the available images are displayed, i selects one of them & it is loaded in the ImageView, when i repeat these steps few time the application throws throws the exception: java.lang.OutOfMemoryError: bitmap size exceeds VM budget 问题:问题是,当我单击按钮时,会显示可用的图像,我选择其中一个并加载到ImageView中,当我重复这些步骤几次时,应用程序抛出异常:java.lang.OutOfMemoryError:位图大小超出VM预算

I goggled it & found that this issue is because of memory leaks but i am unable to find whats wrong with my code. 我仔细检查了一下,发现这个问题是由于内存泄漏引起的,但是我找不到我的代码有什么问题。 Can anyone spend some of his valuable time to help me? 谁能花一些宝贵的时间来帮助我?

Thanks. 谢谢。

If you do a series of setImageURIs , you are loading a series of bitmaps into the view. 如果执行一系列setImageURIs ,则将一系列位图加载到视图中。 If you are getting the java.lang.OutOfMemoryError: bitmap size exceeds VM budget exception , that implies that the imageView does not recycle the previous bitmap when you load a new one. 如果您收到java.lang.OutOfMemoryError: bitmap size exceeds VM budget exception ,则意味着imageView在加载新位图时不会回收前一个位图。 So you 那么你

  • either need to encourage the imageView to recycle the previous bitmap - possibly with setImageView("") 要么需要鼓励imageView回收以前的位图-可能与setImageView("")
  • or get the bitmap from the URI and use setImageBitmap(android.graphics.Bitmap) instead; 或从URI获取位图,并改用setImageBitmap(android.graphics.Bitmap) then you can do setImageBitmap(null) and bitmap.recycle() . 然后可以执行setImageBitmap(null)bitmap.recycle()

Well calling the system.gc() worked!! 好叫system.gc()起作用了!! Before the image is assigned to the imageView, i called the System.gc(), to take care of memory & it works. 在将图像分配给imageView之前,我先调用System.gc()来照顾内存并使其工作。

Look at the modified code: 查看修改后的代码:

public void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    if (resultCode == RESULT_OK) 
    {
         if (requestCode == SELECT_PICTURE) 
         { 
            // calling GC to take care of memory, if you remove this one line of code
            // application will throw the error "VM out of memory error"
            System.gc();
            Uri selectedImageUri = data.getData(); 
            selectedImagePath = getPath(selectedImageUri); 
            imageView.setImageURI(selectedImageUri);  
       }  
   } 
} 

Thanks @Torid . 谢谢@Torid

Can someone comment on it? 有人可以评论吗? is this correct way to call the GC in this situation? 在这种情况下,这是调用GC的正确方法吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.OutOfMemoryError:位图大小超过VM预算 - java.lang.OutOfMemoryError: bitmap size exceeds VM budget 方向改变时,位图大小超出VM预算 - android - OutofMemoryError - bitmap size exceeds VM budget - on orientation change Android:加载图片时出错:OutOfMemoryError:位图大小超过VM预算 - Android: Error loading images: OutOfMemoryError: bitmap size exceeds VM budget java.lang.OutOfMemoryError:位图大小超出VM预算&java.io.FileNotFoundException - java.lang.OutOfMemoryError: bitmap size exceeds VM budget & java.io.FileNotFoundException java.lang.OutOfMemoryError:位图大小超出了Swipey-Viewpager中的VM预算 - java.lang.OutOfMemoryError: bitmap size exceeds VM budget in Swipey-Viewpager java.lang.OutOfMemoryError:位图大小超过第二次运行应用程序时VM预算崩溃 - java.lang.OutOfMemoryError: bitmap size exceeds VM budget crashes on second time running app 如何避免java.lang.OutOfMemoryError:在使用大位图时,位图大小超过了android中的VM预算? - How can I avoid java.lang.OutOfMemoryError: bitmap size exceeds VM budget in android while using a large bitmap? 位图大小超过Vm预算错误android - bitmap size exceeds Vm budget error android 微小图像,无旋转,但仍然出现OutOfMemoryError:位图大小超出VM预算 - Tiny Images, No Rotation, but still get OutOfMemoryError: bitmap size exceeds VM budget 第二次加载后,位图大小超出了VM预算 - Bitmap size exceeds VM budget after second load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM