android中使用位图的问题
我遇到以下问题:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您的用户处于活动 2 时,不要直接转到活动 3(图库)。
首先,完成活动2并将结果返回给活动1。然后开始活动3。
您的第一个/主要活动:
在您的第二个活动中,您必须做这样的事情:
问候
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:
And in your second activity you have to do something like this:
Regards