简体   繁体   中英

Out of Memory on Samsung Galaxy devices

I've really tried my best, but I just can't find a fix for this Problem. I wrote a Soundboard for Android devices, where every sound as well as its button images is loaded dynamically. Here is the crucial paragraph in my code:

private void loadButtonColors(){
    int rescount = AppConfig.BUTTONBACKGROUNDSRES(getApplicationContext()).size();
    SharedPreferences prefs = getSharedPreferences("buttoncolors", 0);
    String colors = null;
    colors = prefs.getString("colors", null);
    List<String> tempList = new ArrayList<String>();
    if(colors != null){
        tempList.addAll(Arrays.asList(TextUtils.split(colors, "~")));
    }
    if(colors == null || tempList.size() != MainActivity.SOUNDBUTTONS.size()){
        for(@SuppressWarnings("unused") SoundButton s : MainActivity.SOUNDBUTTONS){
            int randomint = new Random().nextInt(rescount);
            Integer randominteger = Integer.valueOf(randomint);
            String randomstring = String.valueOf(randominteger);

            tempList.add(randomstring);
        }

        prefs.edit().putString("colors", TextUtils.join("~", tempList)).commit();



    }

    MainActivity.BUTTONCOLORS = (ArrayList<String>) tempList;

}

Later the button's images will be displayed with the images out of the following Arrays where the error seems to arise.

public static ArrayList<Drawable> BUTTONBACKGROUNDSRES(Context context){
    ArrayList<Drawable> tempList = new ArrayList<Drawable>();
    tempList.add(context.getResources().getDrawable(R.color.button_lblue));
    tempList.add(context.getResources().getDrawable(R.color.button_lime));
    tempList.add(context.getResources().getDrawable(R.color.button_orange));
    tempList.add(context.getResources().getDrawable(R.color.button_pink));
    tempList.add(context.getResources().getDrawable(R.color.button_red));
    tempList.add(context.getResources().getDrawable(R.color.button_yellow));
    return tempList;
};

The resulting error looks like this:

java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2150)
at android.content.res.Resources.getDrawable(Resources.java:715)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2132)
at android.content.res.Resources.getDrawable(Resources.java:715)
at com.centivo.inscope21soundboard.AppConfig.BUTTONBACKGROUNDSRES(AppConfig.java:45)
at com.centivo.inscope21soundboard.LauncherActivity.loadButtonColors(LauncherActivity.java:128)
at com.centivo.inscope21soundboard.LauncherActivity.access$1(LauncherActivity.java:127)
at com.centivo.inscope21soundboard.LauncherActivity$1.run(LauncherActivity.java:43)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)

If you wanna check out the App visit the Playstore

EDIT: The size of every button image is about 70kb.

If you are running out of memory, store the tempList in BUTTONBACKGROUNDSRES in a member or static variable. It looks like every time you call BUTTONBACKGROUNDSRES, you are going to load a new set of Drawables. If these are always the same, there is no sense in loading them frequently from scratch and then leaving the tempList to be garbage collected. Just create the list once, then refer it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM