【问题标题】:OutOfMemory Exception when handling images [duplicate]处理图像时出现 OutOfMemory 异常 [重复]
【发布时间】:2011-10-12 13:18:13
【问题描述】:

可能重复:
OutOfMemoryError: bitmap size exceeds VM budget :- Android

我正在编写一个程序,该程序使用图库中的图像,然后将它们显示在一个活动中(一个图像公关活动)。但是,我连续三天一遍又一遍地遇到这个错误,但没有任何消除它的进展:

07-25 11:43:36.197: ERROR/AndroidRuntime(346): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

我的代码流程如下:

当用户按下按钮时,会触发一个指向图库的意图:

 Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
 galleryIntent.setType("image/*");
 startActivityForResult(galleryIntent, 0);

一旦用户选择了图像,图像就会显示在图像视图中:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical">

<ImageView
    android:background="#ffffffff"
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:maxWidth="250dip"
    android:maxHeight="250dip"
    android:adjustViewBounds="true"/>

 </LinearLayout>

在 onActivityResult 方法中我有:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(resultCode == RESULT_OK) {
        switch(requestCode) {
        case 0:             // Gallery
            String realPath = getRealPathFromURI(data.getData());
            File imgFile = new File(realPath);
            Bitmap myBitmap;
            try {
                myBitmap = decodeFile(imgFile);
                Bitmap rotatedBitmap = resolveOrientation(myBitmap);
                img.setImageBitmap(rotatedBitmap);
                OPTIONS_TYPE = 1;
            } catch (IOException e) { e.printStackTrace(); }

            insertImageInDB(realPath);

            break;
        case 1:             // Camera

decodeFile 方法来自here,resolveOrientation 方法只是将位图包装成矩阵并顺时针旋转 90 度。

我真的希望有人可以帮助我解决这个问题。

【问题讨论】:

标签: java android


【解决方案1】:

这是因为你的位图尺寸很大,所以手动减小图像尺寸,或者通过编程方式

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap = BitmapFactory.decodeFile(mPathName, options);

【讨论】:

    【解决方案2】:

    您的 GC 没有运行。尝试分段获取您的位图

    BitmapFactory.Options buffer = new BitmapFactory.Options(); 
    buffer.inSampleSize = 4; 
    Bitmap bmp = BitmapFactory.decodeFile(path, buffer); 
    

    【讨论】:

      【解决方案3】:

      Stackoverflow 中有很多关于bitmap size exceeds VM budget 的问题,所以首先搜索您的问题,当您找不到任何解决方案时,请在此处提问

      【讨论】:

        【解决方案4】:

        问题是因为您的位图尺寸太大,VM 无法处理。例如,从您的代码中,我可以看到您正在尝试将图像粘贴到您使用相机捕获的 imageView 中。所以通常相机图像会太大,这会明显增加这个错误。 因此,正如其他人所建议的那样,您必须通过对图像进行采样或将图像转换为更小的分辨率来压缩图像。 例如,如果您的 imageView 的宽度和高度为 100x100,您可以创建一个缩放位图,以便您的 imageView 被精确填充。你可以这样做,

            Bitmap newImage = Bitmap.createScaledBitmap(bm, 350, 300,true);
        

        或者您可以按照用户 hotveryspicy 建议的方法对其进行采样。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-02
          • 1970-01-01
          相关资源
          最近更新 更多