我的应用程序在源代码中的以下行遇到OOM错误:
image = BitmapFactory.decodeStream(assetManager.open(imgFilename));
Run Code Online (Sandbox Code Playgroud)
就在分配之前导致应用程序被OOM错误杀死:
(...)
08-05 21:22:12.443: I/dalvikvm-heap(2319): Clamp target GC heap from 25.056MB to 24.000MB
08-05 21:22:12.443: D/dalvikvm(2319): GC_FOR_MALLOC freed <1K, 50% free 2709K/5379K, external 18296K/19336K, paused 58ms
08-05 21:22:14.513: D/dalvikvm(2319): GC_EXTERNAL_ALLOC freed <1K, 50% free 2709K/5379K, external 18296K/19336K, paused 101ms
08-05 21:22:14.903: I/dalvikvm-heap(2319): Clamp target GC heap from 25.073MB to 24.000MB
08-05 21:22:14.903: D/dalvikvm(2319): GC_FOR_MALLOC freed 0K, 50% free 2709K/5379K, external 18312K/19336K, paused 53ms
08-05 21:22:22.843: D/ddm-heap(2319): Heap GC request
08-05 21:22:22.963: I/dalvikvm-heap(2319): Clamp target GC heap …Run Code Online (Sandbox Code Playgroud) 我已经阅读了许多关于解码位图的内存分配问题的相关帖子,但即使在使用官方网站提供的代码后仍然无法找到解决以下问题的方法.
这是我的代码:
public static Bitmap decodeSampledBitmapFromResource(InputStream inputStream, int reqWidth, int reqHeight) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
try {
while ((len = inputStream.read(buffer)) > -1) {
baos.write(buffer, 0, len);
}
baos.flush();
InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is1, null, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inPurgeable = true;
options.inInputShareable = true;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeStream(is2, null, …Run Code Online (Sandbox Code Playgroud) 我的应用程序崩溃,在logcat中显示:
java.lang.OutOfMemoryError: (Heap Size=39047KB, Allocated=19932KB)
at android.graphics.BitmapFactory.nativeDecodeFile(Native Method)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:373)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:443)
at com.mApp.mobileapp.mActivity.onActivityResult(mActivity.java:196)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:153)
at android.app.Activity.dispatchActivityResult(Activity.java:4752)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3449)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3503)
at android.app.ActivityThread.access$1100(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1320)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:5109)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:991)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)
在运行此代码时:
String selectedImagePath = data.getStringExtra("imageByteCode");
try {
File imageFile = new File(selectedImagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile
.getAbsolutePath());//line 196
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
base64code = selectedImagePath;
mImage.setImageBitmap(bitmap);
} catch (Exception e) {
} …Run Code Online (Sandbox Code Playgroud)