处理图像时出现内存不足异常 [英] OutOfMemory Exception when handling images

查看:178
本文介绍了处理图像时出现内存不足异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android">OutOfMemoryError:位图的大小超过VM预算: - 安卓

林编写一个程序,它使用的图像从画廊的过程,然后在一个活动(一个图像PR。活动)显示它们。不过,我已经碰到这个错误一遍又一遍了三天没有做消除它的任何进展情况:

Im in the process of writing a program which uses images from the gallery and then displays them in an activity (one image pr. activity). However I've been bumping into this error over and over again for three days straight without doing any progress of eliminating it:

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

我的code流程如下:

My code flow is as follows:

当用户presses一个按钮的意图是发射通往画廊:

When the user presses a button an intent is fired leading to the gallery:

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

在用户已经选择的图像被presented在imageview的图像:

Once the user has selected an image the image is presented in an imageview:

  <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方法我有:

In the onActivityResult method i have:

@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

本德codeFILE的方法是从<一个href="http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">here和resolveOrientation方法只是包装的位图到矩阵,顺时针旋转90度打开它。

The decodeFile method is from here and the resolveOrientation method just wraps the bitmap into a matrix and turns it 90 degrees clockwise.

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

I really hope someone can help me resolve this matter.

推荐答案

那是因为你的位图尺寸较大,所以手动缩小图像尺寸,或者通过编程

it is because your bitmap size is large, so reduce the image size manually, or by programmatically

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

这篇关于处理图像时出现内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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