如何解决java.lang.OutOfMemoryError处理来自Camera的14个数据
我正在制作一个审计应用程序,必须从单选按钮和14个检查点的图像中收集是/否。
该应用程序在装有Android 10设备的设备上运行正常,但是在以某种随机方式收集4-6张图像并在相应的Imageview上显示它们并在String中收集Byte []后,在某些设备上(如在某些牛轧糖设备上)进行测试变量,应用程序也不会在日志中崩溃(现在通过调试器找到它,请参见下文)。看一下相机的代码。恢复活动后,单击照片后该应用程序崩溃了。
public void ClickImageFromCamera(String code) {
Intent CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File destination = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() + ".jpg");
String img_Decodable_Str = String.valueOf(destination);
Log.e("img fromCam",String.valueOf(destination));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(getActivity(),getActivity().getApplicationContext().getPackageName() + ".provider",destination);
CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,uri);
} else {
uri = Uri.fromFile(destination);
CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,uri);
}
CamIntent.putExtra("return-data",true);
startActivityForResult(CamIntent,Integer.parseInt(code));
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
Log.e("Request code",String.valueOf(resultCode));
Log.e("ResultCode ",String.valueOf(resultCode));
if (requestCode == 50) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(50);
} else {
//this is below marshmellow
previewCapturedImage1(50);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),"User cancelled image capture",Toast.LENGTH_SHORT)
.show();
}
}else {
// failed to capture image
// Toast.makeText(getActivity(),"Sorry! Failed to capture image",Toast.LENGTH_SHORT).show();
}
if (requestCode == 51) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(51);
} else {
//this is below marshmellow
previewCapturedImage1(51);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 52) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(52);
} else {
//this is below marshmellow
previewCapturedImage1(52);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 53) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(53);
} else {
//this is below marshmellow
previewCapturedImage1(53);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 54) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(54);
} else {
//this is below marshmellow
previewCapturedImage1(54);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 55) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(55);
} else {
//this is below marshmellow
previewCapturedImage1(55);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 56) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(56);
} else {
//this is below marshmellow
previewCapturedImage1(56);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 57) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(57);
} else {
//this is below marshmellow
previewCapturedImage1(57);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 58) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(58);
} else {
//this is below marshmellow
previewCapturedImage1(58);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 59) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(59);
} else {
//this is below marshmellow
previewCapturedImage1(59);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 60) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(60);
} else {
//this is below marshmellow
previewCapturedImage1(60);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 61) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(61);
} else {
//this is below marshmellow
previewCapturedImage1(61);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 62) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(62);
} else {
//this is below marshmellow
previewCapturedImage1(62);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
if (requestCode == 63) {
if (resultCode == -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//this is above marshmellow
previewCapturedImage(63);
} else {
//this is below marshmellow
previewCapturedImage1(63);
}
} else if (resultCode == 0) {
// user cancelled Image capture
Toast.makeText(getActivity(),Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
}
}
}
public void previewCapturedImage(int code) {
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
// ContentResolver contentResolver = getContentResolver();
InputStream inputStream = null;
try {
inputStream = getActivity().getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// image_stream = mContext.getContentResolver().openInputStream(sendUri);
bitmap = BitmapFactory.decodeStream(inputStream);
bitmap.compress(Bitmap.CompressFormat.JPEG,50,byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
int nh = (int) (bitmap.getHeight() * (512.0 / bitmap.getWidth()));
Bitmap scaled = Bitmap.createScaledBitmap(bitmap,512,nh,true);
scaled.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
/* byte[] imageBytes1 = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(imageBytes1,Base64.NO_WRAP);
Log.e("encode",encoded);*/
Log.e("Original dimensions",bitmap.getWidth() + " " + bitmap.getHeight());
Log.e("Compressed dimensions",scaled.getWidth() + " " + scaled.getHeight());
/*int size = scaled.getRowBytes() * scaled.getHeight();
Log.e("size",String.valueOf(size));
java.nio.ByteBuffer b = java.nio.ByteBuffer.allocate(size);
scaled.copyPixelsToBuffer(b);
byte[] bytes = new byte[size];
Log.e("bytes",String.valueOf(bytes));
*/
if(code ==50) {
imageCode1 = Base64.encodeToString(imageBytes,Base64.NO_WRAP);
Log.e("image1",imageCode1);
set_img_fascia.setImageBitmap(bitmap);
}
if(code ==51) {
imageCode2 = Base64.encodeToString(imageBytes,imageCode2);
set_img_lollypop.setImageBitmap(bitmap);
}
if(code ==52) {
imageCode3 = Base64.encodeToString(imageBytes,imageCode3);
set_img_ATM_Count.setImageBitmap(bitmap);
}
if(code ==53) {
imageCode4 = Base64.encodeToString(imageBytes,imageCode4);
set_img_ATM_Cooling.setImageBitmap(bitmap);
}
if(code ==54) {
imageCode5 = Base64.encodeToString(imageBytes,imageCode5);
set_img_ATM_Backroom_Locked.setImageBitmap(bitmap);
}
if(code ==55) {
imageCode6 = Base64.encodeToString(imageBytes,imageCode6);
set_img_ups_room.setImageBitmap(bitmap);
}
if(code ==56) {
imageCode7 = Base64.encodeToString(imageBytes,imageCode7);
set_img_Branch_Shutter_Damaged.setImageBitmap(bitmap);
}
if(code ==57) {
imageCode8 = Base64.encodeToString(imageBytes,imageCode8);
set_img_Branch_Round_Taken.setImageBitmap(bitmap);
}
if(code ==58) {
imageCode9 = Base64.encodeToString(imageBytes,imageCode9);
set_img_AC_ODU_Copper_Intact.setImageBitmap(bitmap);
}
if(code ==59) {
imageCode10 = Base64.encodeToString(imageBytes,imageCode10);
set_img_Any_Water_Logging_Vincinity.setImageBitmap(bitmap);
}
if(code ==60) {
imageCode11 = Base64.encodeToString(imageBytes,imageCode11);
set_img_FE_Room_Available.setImageBitmap(bitmap);
}
if(code ==61) {
imageCode12 = Base64.encodeToString(imageBytes,imageCode12);
set_img_DG_Door_Locked.setImageBitmap(bitmap);
}
if(code ==62) {
imageCode13 = Base64.encodeToString(imageBytes,imageCode13);
set_img_Other_Obsevation.setImageBitmap(bitmap);
}
if(code ==63) {
imageCode14 = Base64.encodeToString(imageBytes,imageCode14);
set_img_Damage_ATM_lobby.setImageBitmap(bitmap);
}
/*
//decode base64 string to image
imageBytes = Base64.decode(imageCode,Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes,imageBytes.length);
set_img_trainy.setImageBitmap(decodedImage);*/
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public void previewCapturedImage1(int code) {
try {
//encode base64 string to image
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.img);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
options.inSampleSize = 8;
bitmap = BitmapFactory.decodeFile(uri.getPath(),options);
bitmap.compress(Bitmap.CompressFormat.JPEG,byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
// byte[] imageBytes = byteArrayOutputStream.toByteArray();
//imageCode = Base64.encodeToString(imageBytes,Base64.DEFAULT);
// Log.e("image_code",imageCode);
if(code ==50) {
imageCode1 = Base64.encodeToString(imageBytes,imageCode14);
set_img_Damage_ATM_lobby.setImageBitmap(bitmap);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public void previewCapturedImage1(int code) {
try {
//encode base64 string to image
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imageCode14);
set_img_Damage_ATM_lobby.setImageBitmap(bitmap);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
在调试器中发现错误
FATAL EXCEPTION: main
Process: com.field_officer,PID: 15547
java.lang.OutOfMemoryError: Failed to allocate a 14745612 byte allocation with 4194304 free bytes and 8MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:681)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:657)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:701)
at com.field_officer.Fragment.ICICI_report_Static_Fragment.previewCapturedImage(ICICI_report_Static_Fragment.java:794)
at com.field_officer.Fragment.ICICI_report_Static_Fragment.onActivityResult(ICICI_report_Static_Fragment.java:1506)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)
at com.field_officer.HomeActivity.onActivityResult(HomeActivity.java:740)
at android.app.Activity.dispatchActivityResult(Activity.java:6950)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4176)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4223)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1585)
at android.os.Handler.dispatchMessage(Handler.java:110)
解决方法
好,我解决了。
int nh = (int) (bitmap.getHeight() * (512.0 / bitmap.getWidth()));
Bitmap scaled = Bitmap.createScaledBitmap(bitmap,512,nh,true);
scaled.compress(Bitmap.CompressFormat.JPEG,80,byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
Log.e("Original dimensions",bitmap.getWidth() + " " + bitmap.getHeight());
Log.e("Compressed dimensions",scaled.getWidth() + " " + scaled.getHeight());
Log.e("bytes",String.valueOf(bytes));
bitmap =null;
if(code ==50) {
imageCode1 = Base64.encodeToString(imageBytes,Base64.NO_WRAP);
Log.e("image1",imageCode1);
set_img_fascia.setImageBitmap(scaled);
}
if(code ==51) {
imageCode2 = Base64.encodeToString(imageBytes,imageCode2);
set_img_lollypop.setImageBitmap(scaled);
...So on
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。