使用Android相机拍摄数张照片后错误 [英] error after taking several pictures using the android camera
问题描述
我有一个活动A
在那里我建立我自己的camera.In本次活动的摄像头被打开,当使用presses一个按钮图片服用。
I have an activity A
where I'm building my own camera.In this activity the camera is opened and when the use presses a button a picture is taken.
这是这样做的:
Activity A:
//this button needs to be pressed to take the photo
public void onClick(View arg0) {
mCamera.takePicture(null, mPictureCallback, mPictureCallback);
}
//the method that gets called
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
if (imageData != null) {
Intent mIntent = new Intent();
Bundle b = new Bundle();
b.putByteArray("imageData", imageData);
Intent i = new Intent(mContext, ViewPhoto.class);
i.putExtras(b);
startActivity(i);
setResult(FOTO_MODE, mIntent);
finish();
}
}
};
画面被发送到第二个活动,用户可以查看它,看看它是否喜欢它,或者not.If他不喜欢这样,他就可以重拍照片
The picture is sent to a second activity where the user can view it and see if it likes it or not.If he doesn't likes it he can then retake the photo
活动B:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//..........
Bundle extras = getIntent().getExtras();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
byte[] imageData = extras.getByteArray("imageData");
Bitmap 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);
//.......
}
在本次活动我收到图片的字节,并创建第一个位图
:
In this second activity I receive the bytes of the picture and create a first bitmap
:
Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options);
之后,我需要旋转第一个位图
创建第二位:
bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(),
myImage.getHeight(), mat, true);
用户看到后 bitma presult
他可以重拍照片。
backTakePhoto.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...back to activity A
}
});
问题是拿几张照片后,循环,如:
The problem is that after taking several photos, loops like:
activity A->activity B->activity A->Activity B my app crashes in activity B at this line:
bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(),
myImage.getHeight(), mat, true);
这是我的logcat的样子:
And this is how my logcat looks like:
FATAL EXCEPTION: main
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
at com.Xperiaproject.ViewPhoto.onCreate(ViewPhoto.java:71)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)
Force finishing activity com.Xperiaproject/.ViewPhoto
任何想法?
推荐答案
您没有足够的内存来创造你的位图。这就是为什么它会崩溃。
You don't have enough memory to creat your bitmap. That's why it crashes.
要释放你的记忆,你应该回收您的位图时,未使用或将其保存到SD卡上,然后再利用。
To free your memory you should recycle your bitmap when unused or save it to the sd card then recycle.
bitmapResult.recycle();
这篇关于使用Android相机拍摄数张照片后错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!