我面对这个错误:如何检查内存泄漏?以及如何解决这些问题?
FATAL EXCEPTION: main Process: com.petyaar, PID: 18056 java.lang.OutOfMemoryError: Failed to allocate a 63701004 byte allocation with 16777024 free bytes and 40MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:639) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:615) at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391) at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:419) at com.petyaar.OwnerBio.OwnerBioUpdate.onCaptureImageResult(OwnerBioUpdate.java:611) at com.petyaar.OwnerBio.OwnerBioUpdate.onActivityResult(OwnerBioUpdate.java:643) at android.app.Activity.dispatchActivityResult(Activity.java:6508) at android.app.ActivityThread.deliverResults(ActivityThread.java:3702) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3749) at android.app.ActivityThread.access$1400(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5441) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
我不知道如何解决这个问题,我也一直在监测的“Android监视器”我的内存分配。内存分配不断增加,即使我没有与应用程序交互。它高达500MB +。
这就是我通过摄像头拍摄的图像。
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = null;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//THIS IS WHAT YOU WANT!
String capturedImageFilePath = cursor.getString(column_index_data);
filename = capturedImageFilePath.substring(capturedImageFilePath.lastIndexOf("/") + 1);
thumbnail = BitmapFactory.decodeFile(capturedImageFilePath);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 50, bytes);
thumbnail=Bitmap.createScaledBitmap(thumbnail, 200, 300, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Log.i("final camera size", String.valueOf(thumbnail.getAllocationByteCount()));
}
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
byte[] byteArray = bytes.toByteArray();
encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
Log.e("base64string name", encoded);
Log.e("Image name", capturedImageFilePath);
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
profile_image.setImageBitmap(thumbnail);
}
请告诉我如何解决此问题。
在应用程序标签的AndroidManifest.xml文件中放上这个标签android:largeHeap =“true” –
为什么这样? 尽管我已经阅读过,但在android:largeHeap =“true”中也存在其他问题。 和不断增加内存大小的原因是什么? – rajat44
大堆的原因[检查此链接](http://stackoverflow.com/questions/27396892/what-are-advantages-of-setting-largeheap-to-true) –