Every time I load certain activity called ProductActivity, I encounter the following error:
Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 15694612 byte allocation with 2874400 free bytes and 2MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java)
at android.graphics.BitmapFactory.nativeDecodeAsset(BitmapFactory.java)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:446)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at android.content.res.Resources.createFromResourceStream(Resources.java:2813)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2514)
at android.content.res.Resources.loadDrawable(Resources.java:2416)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:393)
at android.content.res.TypedArray.getDrawable(TypedArray.java:751)
at android.view.View.<init>(View.java:3740)
at android.widget.ImageView.<init>(ImageView.java:139)
at android.widget.ImageView.<init>(ImageView.java:135)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1017)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1076)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:729)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
at demand.inn.com.quflip.activity.ProductInfoActivity.onCreate(ProductInfoActivity.java:73)
at android.app.Activity.performCreate(Activity.java:6041)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$800(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5273)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Now how should I optimize the resources so that I can get rid of this error?
Should I resize the images on the server or should I resize them inside my app to get free from all these.
Currently, I am using picasso to load the images inside my app.
You are trying to load 15694612 bytes = 15 MB weigh image, which is too big. If there is no option to have a separate downsampled resource, then your option is to downsample it programmatically and then load it.
private Bitmap downscaleBitmapUsingDensities(final int sampleSize, final int imageResId) {
final Options bitmapOptions = new Options();
bitmapOptions.inDensity = sampleSize;
bitmapOptions.inTargetDensity = 1;
final Bitmap scaledBitmap = BitmapFactory.decodeResource(getResources(), imageResId, bitmapOptions);
scaledBitmap.setDensity(Bitmap.DENSITY_NONE);
return scaledBitmap;
}
sampleSize is an integer which specifies how many times you want the source bitmap to be downsampled before loading it into memory.
As you are using picasso to load the images, you can get the bitmap from the picasso by using following code:
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// do you compression logic here and apply that bitmap to image
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
void foo() {
Picasso.with(getContext()).load(getUrl()).into(target);
}
The answer provided by #azizbekian will give you scaled version of image, you can further compress it by using simple code mentioned in this link, which considers the orientation, aspect ration of image too.
Related
In my layout applications I have a card view where each card contains a representations of an application.
In each card there are: the name of an applications, a description and his icon.
#Override
public void onBindViewHolder(#NonNull final AdapterForCard.ViewHolder holder, final int position) {
holder.iv.setImageDrawable(myCards.get(position).getIcon());
holder.titleView.setText(myCards.get(position).getTitle());
holder.descView.setText(myCards.get(position).getInfo());
holder.cv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(holder.cv.getContext(), "Cliccked card: "+(holder.titleView.getText()), Toast.LENGTH_SHORT).show();
Context context = v.getContext();
Intent i = new Intent(context,DetailsAppActivity.class);
//First Convert Image into Byte Array
// and then pass into Intent and in next activity
// get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView
// holder.iv.getId_ 2131296339 v.getId_ 2131296348.
//
int id1 = holder.iv.getId();
int id2 = v.getId();
int id3 = R.id.iconView;
System.out.println("id1_ "+id1);
System.out.println("id2_ "+id2);
System.out.println("id3_ "+id3);
Bitmap btm = BitmapFactory.decodeResource(v.getResources(),id1);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
System.out.println("bitmap is null ? "+(btm==null));
btm.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
i.putExtra("title", ""+holder.titleView.getText());
i.putExtra("title", ""+holder.titleView.getText());
i.putExtra("icon",byteArray);
context.startActivity(i);
}
});
}
I want pass the three date to my DetailsActivity, but i have the follow error:
id1_ 2131296339
id2_ 2131296348
id3_ 2131296339
bitmap is null ? true
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.salvatorefiorilla.systemmonitor, PID: 6151
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.salvatorefiorilla.systemmonitor.AdapterForCard$1.onClick(AdapterForCard.java:90)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19858)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
How to set id correctly?
Update 2
As you told that you don't have resource id. You have application package, and have to get application icon, so you can pass package name of that app. And in next activity just get application icon.
Important Edit
I did not notice that you are using Drawable object instead of using an int id. There is no need to store drawable objects in List. So do the following.
First change type of icon as int (Resource id).
list.setIconId(R.drawable.ic_launcher)
BitmapFactory.decodeResource(getResources(), myCards.get(position).getIconId());
set ImageView image by below method.
Old solution is also edited for same.
holder.iv.setImageResource(myCards.get(position).getIconId());
Solution
Just change holder.iv.getId(); with myCards.get(position).getIconId()
Bitmap btm = BitmapFactory.decodeResource(v.getResources(),myCards.get(position).getIconId());
Reason of crash
Because holder.iv.getId() returns you ImageView id NOT drawable resource id.
Suggestion (must use)
Pass resource id instead of passing bitmap array. There is no need to pass byte array of bitmap.
i.putExtra("icon",myCards.get(position).getIconId());
in next activity
imageView.setImageResource(getIntent().getIntExtra("icon",0));
Isn't it simple :)
well your stream is null because you are giving id instead of position. use this one
mCard.get(position).getIcon()
Context
I'm working on a small test code right now. It selects an image, resizes it to fit the screen based on the screen density. While it does this, it makes a copy of the image at the size it would be in an xxhdpi display, and the converts into a Bitmap, then into a String. The String is then carried through an Intent to the next screen where the String is turned back into a Bitmap and then placed in an ImageButton.
This worked fine until I added in a chunk of code to resize the image given by the string based on the density. The odd thing is I copied and pasted the DPI() method from another activity and there were no issues in that activity.
Code
package com.example.zachary.imagetesting.Resizing;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import com.example.zachary.imagetesting.R;
public class PictureDecoded extends Activity {
TextView Text;
ImageButton Image;
String string;
Bitmap picture;
public float DPI(){return getResources().getDisplayMetrics().density;}
float dpi = DPI() * 180;
int size = (int) dpi;
public Bitmap Pic(String string){
try{
byte[] b = Base64.decode(string, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resizing);
string = getIntent().getStringExtra("String");
Image = (ImageButton) findViewById(R.id.image);
Text = (TextView) findViewById(R.id.text);
picture = Pic(string);
Image.setImageBitmap(Bitmap.createScaledBitmap(picture,size,size,false));
}
public void next(View view){ }
// I also tried addring DPI() here as well to no success.
//public float DPI(){return getResources().getDisplayMetrics().density;}
}
Error Log
01-15 19:55:17.104 30570-30570/com.example.zachary.imagetesting D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-15 19:55:17.194 30570-30570/com.example.zachary.imagetesting E/Bitmap String:: /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAGVAhwDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDuKKKK
/wAVz/UgKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK
ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA
KKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAo
oooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACii
igAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK
ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA
KKKKACiiigAooooAKKKKACiiigA45GemM+3XGeTjOOPx5ODkruPAuv6DpWppYeMtHj1zwhqk0MOt
2qxqur2EO9h/bHhy/UpcadqtlkTeXFKLTU4l+wanBNA0RTtPjP8ABnU/hRqdldW102ueC/EUa3nh
XxK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ZW/eU
Ks6cmoybUZcskpwvq4T5oSTalFq97hOdN81OcoPvFtXt3s9V5PTvc/I34r/so+Pfh6l7rHh/d408
L2y+c93YW7R67YQguJG1HRlMrzRQgAveaZLdRiMNPcwWkQc19r/shaNcaR8DtFmuYmhfW9W17W1R
1KsYZL4aZbSEH+Ga20mKeMnJMcgOSDX03gf5/H/Pr05OKZHHHFGsUSpFGoCqqIERRuP3UQAAclsK
PzJ4+kzXjLMs6yWnlWYU6VSrTxVKu8bBeznVp06deHs6tGK9nzuVRTdSlyRaUYOjf94+mrjKlagq
NRJtSUvaLRtRUrJxStf3m7rva29/xq/akvY7749+P3iZWS2m0KyLD/n
01-15 19:55:17.194 30570-30570/com.example.zachary.imagetesting I/Timeline: Timeline: Activity_launch_request id:com.example.zachary.imagetesting time:50420468
01-15 19:55:17.244 30570-30570/com.example.zachary.imagetesting D/AndroidRuntime: Shutting down VM
01-15 19:55:17.244 30570-30570/com.example.zachary.imagetesting E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zachary.imagetesting, PID: 30570
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.zachary.imagetesting/com.example.zachary.imagetesting.Resizing.PictureDecoded}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2546)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:90)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
at com.example.zachary.imagetesting.Resizing.PictureDecoded.DPI(PictureDecoded.java:20)
at com.example.zachary.imagetesting.Resizing.PictureDecoded.<init>(PictureDecoded.java:21)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1650)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2536)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
The large block of text is the String converted from Bitmap that was passed to the Activity. I left it in just in case it was useful. The line before the large block of text was the button click that started the intent to the activity which crashes due to NullPointerException.
I can add the other activities on request, but since it worked before I tried resizing based on screen density I didn't feel it was needed.
An Activity's base Context won't have been setup until after onCreate() has finished executing. Simply move the initialization of dpi to onCreate() after super.onCreate() has been called.
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private class AsyncImageLoader extends AsyncTask<String, Void, Bitmap[]> {
Bitmap bitmap[];
protected void onPreExecute() {
pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));
showDialog();
}
#Override
protected Bitmap[] doInBackground(String... params) {
hideDialog();
bitmap = new Bitmap[2];
bitmap[0] = getBitmapFromURL(params[0]);
bitmap[1] = getBitmapFromURL(params[1]);
return bitmap;
}
#Override
protected void onPostExecute(Bitmap[] bm) {
imgCover.setImageBitmap(bm[1]);
bm[1].recycle();
imgProfile.setImageBitmap(bm[0]);
bm[0].recycle();
if (MainActivity.PROFILE_UID.equals(MainActivity.USER_UID))
FragmentDrawer.imgProfileNavDrawer.setImageBitmap(bm[0]); // Sol drawer' da çıkan yuvarlak resmi güncellemek için
}
}
01-10 21:11:14.621 7906-8194/project.com.holobech E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:528)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:600)
at project.com.holobech.activity.ProfileFragment.getBitmapFromURL(ProfileFragment.java:508)
at project.com.holobech.activity.ProfileFragment$AsyncImageLoader.doInBackground(ProfileFragment.java:529)
at project.com.holobech.activity.ProfileFragment$AsyncImageLoader.doInBackground(ProfileFragment.java:516)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
It works perfect for Android 5.0 and higher versions. But when i try with 4.2.2 version, that error occure. How can i fix this problem ?
Thanks in advice
This is very common error once you deal with bitmap. Converting bitmap directly from URL is not a good practice. You should use bitmap BitmapFactory.Options feature provided by android by which you can get the size of the bitmap without even creating a bitmap. Now you have to use insamplesize of BitmapFactory Options and re size the images. I guess the size and the quality of image is too high and that's why when you are converting it into bitmap it gives you out of memory. Each Android app has max 50 mb of RAM to use once it cross beyond that limit android system throws out of memory exception. However we can manage it by making an entry of an attribute in manifest in application tag largeHeap to true.
The best practice to deal with android bitmap is available on developer site. You can click here
Note:- largeHeap feature will work only from OS level 3.0 and above.
Hi I am trying to create an android application to display images so that you can click on a 'next' button and then the next image shows. So far I have this method
public void buttonClick(){
imgView = (ImageView)findViewById(R.id.imgView);
buttonS = (Button)findViewById(R.id.button);
buttonS.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
current_image_index++;
current_image_index = current_image_index % images.length;
imgView.setImageResource(images[current_image_index]);
}
}
);
}
However when I try to run this, I get a java.lang.OutOfMemoryError when I click the 'next' button twice. Am I doing something wrong? My image array looks like this:
int[] images = {R.drawable.img};
I have tried changing the Manifest file to include
android:largeHeap="true"
This is where I set the variables and call the method above
public class MainActivity extends AppCompatActivity {
private static ImageView imgView;
private static Button buttonS;
private int current_image_index;
int[] images = {R.drawable.dog,R.drawable.img};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick();
}
The stack trace is:
08-29 23:17:34.781 15450-15450/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.daniel.firstapp, PID: 15450
java.lang.OutOfMemoryError: Failed to allocate a 2070252108 byte allocation with 16777216 free bytes and 482MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:816)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:637)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1019)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3778)
at android.content.res.Resources.loadDrawable(Resources.java:3651)
at android.content.res.Resources.getDrawable(Resources.java:1865)
at android.content.Context.getDrawable(Context.java:408)
at android.widget.ImageView.resolveUri(ImageView.java:752)
at android.widget.ImageView.setImageResource(ImageView.java:408)
at com.example.daniel.firstapp.MainActivity$1.onClick(MainActivity.java:40)
at android.view.View.performClick(View.java:5217)
at android.view.View$PerformClick.run(View.java:20983)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6141)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
It is because the assigned heap for the application to handle the memory for all the images is low. You can increase it in the Android manifest file.
specify
android:largeHeap="true"
as a property of <application> tag
eg:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:largeHeap="true"
android:theme="#style/AppTheme" >
and see if that fixes the issue.
One option is to scale down the images before you display it.
Recycle the ImageView before you add the new image (But this will not hold up if the image is very large).
my app is throwing outofmemory exceptions to lots of people using it. my app creates a bitmap of the current screen then animates it to transition between pages.
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4222)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4217)
... 10 more
Caused by: android.view.InflateException: Binary XML file line #118: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at com.de_veloper.businessbuilder.MainActivity.premiumMenu(MainActivity.java:558)
... 13 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:614)
... 23 more
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 8683212 byte allocation with 6867876 free bytes and 6MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:726)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:547)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1014)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3723)
at android.content.res.Resources.loadDrawable(Resources.java:3596)
at android.content.res.TypedArray.getDrawable(TypedArray.java:750)
at android.widget.ImageView.<init>(ImageView.java:151)
at android.widget.ImageView.<init>(ImageView.java:140)
at android.widget.ImageView.<init>(ImageView.java:136)
... 26 more
the code that creates the bitmap
public static Bitmap loadBitmapFromView(View v) {
Bitmap bitmap;
v.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v.getDrawingCache());
bitmap.setHasAlpha(false);
v.setDrawingCacheEnabled(false);
return bitmap;
}
the code that animates the bitmap
public void mainMenu(View view) {
$isonfirstpage = 0;
$isonmainmenu = 1;
$isonpremiummenu = 0;
final RelativeLayout backgroundLayout = (RelativeLayout) findViewById(R.id.activity_background);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout newLayout = (RelativeLayout)inflate.inflate(R.layout.main_menu, null);
Bitmap bitmap = loadBitmapFromView(backgroundLayout);
Drawable d = new BitmapDrawable(getResources(), bitmap);
backgroundLayout.removeAllViewsInLayout();
View mainmenuView = findViewById(R.id.mainmenuView);
View firstpageView = findViewById(R.id.firstpageView);
final ImageView screenie = new ImageView(getApplicationContext());
screenie.setImageDrawable(d);
backgroundLayout.addView(newLayout);
backgroundLayout.addView(screenie);
screenie.animate()
.translationY(backgroundLayout.getHeight())
.setDuration(300);
new Handler().postDelayed(new Runnable() {
public void run() {
backgroundLayout.removeView(screenie);
}
}, 1500);
}
when ever the Bitmap size is huse then you will get OutOfMemory Exception you can decode/scale the bitmap when ever you are setting it to the imageview.
public Bitmap decodeUri(Uri path) throws FileNotFoundException
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(path),null,o);
int REQUIRED_SIZE = 100;
int width_temp = o.outWidth;
int height_temp = o.outHeight;
int scale = 1;
while (true)
{
if(width_temp/2 < REQUIRED_SIZE || height_temp/2<REQUIRED_SIZE)
{
break;
}
width_temp/=2;
height_temp/=2;
scale*=2;
}
BitmapFactory.Options o1 =new BitmapFactory.Options();
o1.inSampleSize = scale;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(path),null,o1);
}
you can covert your image path to Uri as follows
decodeUri(Uri.fromFile(new File("your file path")))