android中使用位图的问题

发布于 2024-12-02 17:34:22 字数 1291 浏览 0 评论 0原文

我遇到以下问题:

First activity

Android 相机已打开,用户可以拍照。拍照后,将启动第二个活动。

Second activity

在此活动中,用户可以查看照片。 为此,我从第一个活动收到图片,如下所示:

Bundle extras = getIntent().getExtras();

并创建以下位图:

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;
        options.inDither = true; 
        byte[] imageData = extras.getByteArray("imageData");
        myImage = BitmapFactory.decodeByteArray(imageData, 0,
                imageData.length, options);

        Matrix mat = new Matrix();
        mat.postRotate(90);
        bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(),
                myImage.getHeight(), mat, true);
        //...operations on the bitmapResult

结果位图存储到 sdcard 中,在离开此活动之前,我回收我的位图以避免内存问题:

bitmapResult.recycle();
bitmapResult=null;

完成所有这些后,我们继续进行活动 3。

Third activity

在此活动中,用户可以看到不同尺寸的图片。图片是从 sdcard 中获取的。

最大的问题是,当用户点击后退按钮时,活动从活动 转移到活动 ,但我的应用程序崩溃了,因为正在尝试对位图进行操作

如果我不在第二个活动中回收位图,当我在应用程序中上下几次时,我会收到 OutOfMemeory Exception

有什么想法吗?

I have the following issue:

First activity

The android camera is opened and the user can take a picture.Once the picture is taken the second activity is launched.

Second activity

In this activity the user has the posibility to view the photo.
For that I receive from the first activity the picture, like this:

Bundle extras = getIntent().getExtras();

and I create the following bitmap:

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;
        options.inDither = true; 
        byte[] imageData = extras.getByteArray("imageData");
        myImage = BitmapFactory.decodeByteArray(imageData, 0,
                imageData.length, options);

        Matrix mat = new Matrix();
        mat.postRotate(90);
        bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(),
                myImage.getHeight(), mat, true);
        //...operations on the bitmapResult

The result bitmap is the stored into the sdcard and before leaving this activity I recycle my bitmap in order to avoid memory issue:

bitmapResult.recycle();
bitmapResult=null;

After all this is done we move on to activity 3.

Third activity

In this activity the user can see the picture in different size.The picture is taken from the sdcard.

The big problem is when the user hits the back button is taken from the activity three to activity two, but my app crashes because is trying to do operations on bitmap that has been recycled.

And if I don't recycle the bitmap in the second activity I get OutOfMemeory Exception when I go a few times up and down through my application.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖迟 2024-12-09 17:34:23

当您的用户处于活动 2 时,不要直接转到活动 3(图库)。
首先,完成活动2并将结果返回给活动1。然后开始活动3

您的第一个/主要活动:

public class TakeAPicture extends Activity {

    public static final int REQUEST_PICTURE_WAS_TAKEN = 100;
    public static final int REQUEST_SHOW_GALLERY = 101;
    public static final int RESULT_PICTURE_WAS_TAKEN_SHOW_GALLERY = 102;
    public static final int RESULT_BACK_FROM_GALLERY = 103;

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

        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(),
                        PictureTaken.class);
                startActivityForResult(i, REQUEST_PICTURE_WAS_TAKEN);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case REQUEST_PICTURE_WAS_TAKEN:
            if (resultCode == RESULT_PICTURE_WAS_TAKEN_SHOW_GALLERY) {
                Intent i = new Intent(getApplicationContext(), Gallery.class);
                startActivityForResult(i, REQUEST_SHOW_GALLERY);
            }
            break;
        default:
            break;
        }
    }
}

在您的第二个活动中,您必须做这样的事情:

public class PictureTaken extends Activity {

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

        Button btn = (Button) findViewById(R.id.button2);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                PictureTaken.this
                        .setResult(TakeAPicture.RESULT_PICTURE_WAS_TAKEN_SHOW_GALLERY);
                PictureTaken.this.finish();
            }
        });
    }
}

问候

When your user is in Activity 2 don't go directly to Activity 3 (the gallery).
First, finish Activity 2 and return a result to Activity 1. Then start Activity 3.

Your first/main activity:

public class TakeAPicture extends Activity {

    public static final int REQUEST_PICTURE_WAS_TAKEN = 100;
    public static final int REQUEST_SHOW_GALLERY = 101;
    public static final int RESULT_PICTURE_WAS_TAKEN_SHOW_GALLERY = 102;
    public static final int RESULT_BACK_FROM_GALLERY = 103;

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

        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(),
                        PictureTaken.class);
                startActivityForResult(i, REQUEST_PICTURE_WAS_TAKEN);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case REQUEST_PICTURE_WAS_TAKEN:
            if (resultCode == RESULT_PICTURE_WAS_TAKEN_SHOW_GALLERY) {
                Intent i = new Intent(getApplicationContext(), Gallery.class);
                startActivityForResult(i, REQUEST_SHOW_GALLERY);
            }
            break;
        default:
            break;
        }
    }
}

And in your second activity you have to do something like this:

public class PictureTaken extends Activity {

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

        Button btn = (Button) findViewById(R.id.button2);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                PictureTaken.this
                        .setResult(TakeAPicture.RESULT_PICTURE_WAS_TAKEN_SHOW_GALLERY);
                PictureTaken.this.finish();
            }
        });
    }
}

Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文