java - 处理图像时出现 OutOfMemory 异常

标签 java android

<分区>

Possible Duplicate:
OutOfMemoryError: bitmap size exceeds VM budget :- Android

我正在编写一个程序,该程序使用图库中的图像,然后在 Activity 中显示它们(一个图像 pr. Activity )。然而,我已经连续三天一遍又一遍地遇到这个错误,没有任何消除它的进展:

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

我的代码流程如下:

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

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

一旦用户选择了图像,图像就会显示在 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 方法中:

@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 度。

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

最佳答案

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

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

关于java - 处理图像时出现 OutOfMemory 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6815899/

相关文章:

java - 如何将 spring bean 注入(inject) jsp 2.0 SimpleTag?

java - jar 中的哪个文件读取参数

android - Android 中查询中的描述 LIMIT

java - System.loadLibrary 卡住,永远不会返回

android - 如何通过 Android 在 Facebook 上分享带有 CAPTION 的照片?

android - 减小 apk 大小

java - 如何设置带有 UTC 时间的日历?

java - 更多关于 equals 和 hashCode 的内容

android - 使用 SoundManager 播放多种声音

java - mysql 查询不是在 java 中执行而是在我的 SQLYog 中执行