在连续获得相同的图像两次后出内存不足的Andr​​oid位错误 [英] android bitmap out-of-memory error after getting same image twice in a row

查看:157
本文介绍了在连续获得相同的图像两次后出内存不足的Andr​​oid位错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该抢到从用户的图片库中的图像,然后在随后的活动显示图像的程序。由于我测试的一部分,我尝试做操作连续两次:在ViewImageActivity我点击那个带我到画廊的按钮;选择一个图像;然后看到好友活动伙伴的形象。然后我点击后退按钮,并重复上述过程。第二次左右,我总是得到一个 ava.lang.OutOfMemoryError 。我包括错误日志:

I have a program that is supposed to grab an image from a user's gallery and then display the image in a subsequent activity. As part of my testing, I try to do the operation twice in a row: in ViewImageActivity I click on the button that takes me to the gallery; pick an image; then see the image in FriendsActivity. Then I click the back button and repeat the process. The second time around I always get a ava.lang.OutOfMemoryError. I am including the error log:

04-26 09:43:57.911: W/dalvikvm(30841): threadid=1: thread exiting with uncaught exception (group=0x40c231f8)
04-26 09:43:57.918: E/AndroidRuntime(30841): FATAL EXCEPTION: main
04-26 09:43:57.918: E/AndroidRuntime(30841): java.lang.OutOfMemoryError
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at com.example.game.utils.FileUtils.imageFromGallery(FileUtils.java:87)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at com.example.game.utils.FileUtils.unmarshallBitmap(FileUtils.java:71)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at com.example.game.FriendsActivity.onCreate(FriendsActivity.java:46)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.app.Activity.performCreate(Activity.java:4638)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1940)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2001)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.app.ActivityThread.access$600(ActivityThread.java:129)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1153)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.os.Looper.loop(Looper.java:137)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at android.app.ActivityThread.main(ActivityThread.java:4516)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at java.lang.reflect.Method.invokeNative(Native Method)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at java.lang.reflect.Method.invoke(Method.java:511)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-26 09:43:57.918: E/AndroidRuntime(30841):    at dalvik.system.NativeStart.main(Native Method)
04-26 09:44:00.997: I/Process(30841): Sending signal. PID: 30841 SIG: 9

我的程序是这样写这样:

My program is written like so:

ViewImageActivity派遣意图摆脱图片库的照片;然后调用的onActivityResult好友活动伙伴。好友活动伙伴依次调用fileutils中的 unmarshallBitmap 静态方法来获取图像进行显示。请参见下面的代码片段:

ViewImageActivity dispatch intent to get photo from image gallery; then onActivityResult calls FriendsActivity. FriendsActivity in turn calls FileUtils's static method unmarshallBitmap to get the image for displaying. See following snippets:

ViewImageActivity:

ViewImageActivity:

public void dispatchGalleryIntent(View view) {
    Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(gallery, LOAD_IMAGE_REQUEST_CODE);
}

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

    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == LOAD_IMAGE_REQUEST_CODE) {
            imageUri = data.getData();
        }
        dispatchIntentToFriendsActivity();
    }
}

private void dispatchIntentTo FriendsActivity() {
    Intent intent = new Intent(this, FriendsActivity.class);
    intent.putExtra(IMAGE_URI, imageUri);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

好友活动伙伴

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_friends);
    Uri imageUri = (Uri) getIntent().getExtras().get(IMAGE_URI);
    myImage = FileUtils.unmarshallBitmap(imageUri, getContentResolver());

      ...
}

文件实用

public static Bitmap unmarshallBitmap(Uri imageUri, ContentResolver resolver) {
   return imageFromGallery(imageUri, resolver);
}

private static Bitmap imageFromGallery(Uri imageUri, ContentResolver resolver) {
        try {
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = resolver.query(imageUri, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            return BitmapFactory.decodeFile(picturePath);
        } catch (Exception x) {
            return null;
        }
    }

BTW:我已经看了<一个href=\"http://stackoverflow.com/questions/12016716/how-to-solve-the-out-of-memory-issue-while-displaying-image-in-android?rq=1\">How解决了内存不足的问题,而在Android中显示图像?。我的图像加载罚款的第一次。问题是,如果用户改变他们的主意,决定选择不同的图像(后退键和重复),然后应用程序崩溃如上所述。

BTW: I already saw How to solve the Out of Memory issue while displaying image in android?. My image loads fine the first time. The problem is if user change their mind and decide to choose a different image (back button and repeat), then the app crash as explained above.

推荐答案

您应该由options.inJustDe codeBounds = true,则加载它prepare用于显示图像;然后调整的采样大小。

You should prepare the image for display by loading it with options.inJustDecodeBounds = true; and then adjusting the samplesize.

请参阅: http://developer.android.com/training/displaying-bitmaps/

除此之外,你叫bitmap.recycle(),当你不再需要的位图?第一个可能仍然是在内存中,当你加载第二,导致错误。

Apart from that, are you calling bitmap.recycle() when you no longer need the bitmap? The first could still be in memory when you load the second, causing the error.

这篇关于在连续获得相同的图像两次后出内存不足的Andr​​oid位错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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