I want to show the Bitmap image in ImageView from sd card which is stored already. 我想从已存储的SD卡中的ImageView中显示位图图像。 After run my application is crash and getting OutOfMemoryError error of: 运行后,我的应用程序崩溃并且出现OutOfMemoryError错误:
(java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM) (java.lang.OutOfMemoryError:未能分配23970828字节分配和2097152可用字节,以及2MB直到OOM)
I have no idea or why its out of memory. 我不知道,也不知道为什么它内存不足。 I think my image size is very large so I tried to change it. 我认为我的图片尺寸很大,因此我尝试对其进行更改。
Iterator<String> it = imageArray.iterator();
while (it.hasNext()) {
Object element = it.next();
String objElement = element.toString();
Log.e("objElement ", " = " + objElement);
final ImageView imageView = new ImageView (getContext());
final ProgressBar pBar = new ProgressBar(getContext(), null,
android.R.attr.progressBarStyleSmall);
imageView.setTag(it);
pBar.setTag(it);
imageView.setImageResource(R.drawable.img_placeholder);
pBar.setVisibility(View.VISIBLE);
if (objElement.endsWith(mp3_Pattern)) {
Log.e("Mp3 ", " ends with ");
pBar.setVisibility(View.GONE);
imageView.setImageResource(R.drawable.audio_control);
}
if (objElement.endsWith(png_Pattern)) {
Bitmap bitmap = BitmapFactory.decodeFile(objElement);
int size = Math.min(bitmap.getWidth(), bitmap.getHeight());
int x = (bitmap.getWidth() - size) / 2;
int y = (bitmap.getHeight() - size) / 2;
Bitmap bitmap_Resul = Bitmap.createBitmap(bitmap, x, y, size, size);
Log.e("bitmap_Resul "," = "+ bitmap_Resul);
if (bitmap_Resul != bitmap) {
bitmap.recycle();
}
imageView.setImageBitmap(bitmap_Resul);
Log.e("png_Pattern ", " ends with ");
Log.e(" bitmap "," = " + bitmap);
}
holder.linearLayout.addView(imageView);
holder.linearLayout.addView(pBar);
The log cat information: 日志猫信息:
08-27 14:11:15.307 1857-1857/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tazeen.classnkk, PID: 1857
java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:812)
at android.graphics.Bitmap.createBitmap(Bitmap.java:789)
at android.graphics.Bitmap.createBitmap(Bitmap.java:709)
at android.graphics.Bitmap.createBitmap(Bitmap.java:634)
at com.example.tazeen.classnkk.AllPosts_Page$MyListAdapter.getView(AllPosts_Page.java:357)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1659)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
#1楼
参考:https://stackoom.com/question/2BIMF/Android-java-lang-OutOfMemoryError-无法分配-字节分配与-可用字节和-MB直到OOM
#2楼
have you tried adding this to your manifest under applications? 您是否尝试过将其添加到应用程序下的清单中? android:largeHeap="true"
? android:largeHeap="true"
吗?
like this 像这样
<application
android:name=".ParaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:largeHeap="true" >
#3楼
OutOfMemoryError is the most common problem occured in android while especially dealing with bitmaps. OutOfMemoryError是android中最常见的问题,尤其是在处理位图时。 This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space. 当由于内存不足而无法分配对象时,Java虚拟机(JVM)会引发此错误,并且垃圾回收器无法释放一些空间。
As mentioned by Aleksey, you can add below entities in your manifest file android:hardwareAccelerated="false"
, android:largeHeap="true"
it will work for some environment's. 如Aleksey所述,您可以在清单文件android:hardwareAccelerated="false"
和android:largeHeap="true"
中将以下实体添加到某些环境中。
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
you should definately read some of Androids Developer concept's, specially here: Displaying Bitmaps Efficiently 您应该明确阅读一些Android Developers概念,特别是在这里: 有效显示位图
Read all 5 topics and rewrite your code again. 阅读所有5个主题,然后再次重写代码。 If it still doesn't work we will be happy to see what you've done wrong with the tutorial material. 如果仍然无法正常工作,我们将很高兴看到您在本教程资料中做错了什么。
Here some of possible answers for these type of errors in SOF 这是SOF中此类错误的一些可能答案
Android: BitmapFactory.decodeStream() out of memory with a 400KB file with 2MB free heap Android:BitmapFactory.decodeStream()内存不足,带有400KB文件和2MB可用堆
How to solve java.lang.OutOfMemoryError trouble in Android 如何解决Android中的java.lang.OutOfMemoryError问题
Android : java.lang.OutOfMemoryError Android:java.lang.OutOfMemoryError
java.lang.OutOfMemoryError java.lang.OutOfMemoryError
Solution for OutOfMemoryError: bitmap size exceeds VM budget OutOfMemoryError的解决方案:位图大小超出VM预算
Edit: From the comments of @cjnash 编辑:从@cjnash的评论
For anyone that still had crashes after they added this line, try sticking your image into your res/drawable-xhdpi/ folder instead of your res/drawable/ and it might resolve your issue. 对于添加此行后仍然崩溃的任何人,请尝试将图像粘贴到res / drawable-xhdpi /文件夹中,而不是res / drawable /文件夹中,这可能会解决您的问题。
#4楼
Resize your image before setup to ImageView like this: 在设置为ImageView之前,请调整图像大小 ,如下所示:
Bitmap.createScaledBitmap(_yourImageBitmap, _size, _size, false);
where size is actual size of ImageView. 其中size是ImageView的实际大小。 You can reach size by measuring: 您可以通过测量达到尺寸:
imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
and use next size imageView.getMeasuredWidth()
and imageView.getMeasuredHeight()
for scaling
. 并使用下一个大小的imageView.getMeasuredWidth()
和imageView.getMeasuredHeight()
进行scaling
。
#5楼
For me, the problem was that my .png file was being de-compressed to be a really huge bitmap in memory, because the image had very large dimensions (even though the file size was tiny). 对我来说,问题在于我的.png文件被解压缩为内存中真正的巨大位图,因为图像的尺寸非常大(即使文件很小)。
So the fix was to simply resize the image :) 所以解决方法是简单地调整图像大小:)
#6楼
使用Glide Library和Override尺寸减小尺寸;
Glide.with(mContext).load(imgID).asBitmap().override(1080, 600).into(mImageView);