Problem mit outofmemory Fix mit MemoryCaching. Nur wie ?

  • Antworten:12
  • Bentwortet
deeprojects
  • Forum-Beiträge: 35

04.01.2014, 16:18:01 via Website

Hi Leute,
habe eine großes Problem,
arbeite an einer App wo viele Bilder geladen werden also direkt zum Start.
Der Startscreen z.B. besteht aus einem Custom Background ein Paar ImageViews und ImageButtons.
Wenn ich diesen laden will kommt der java.lang.OutOfMemory error. Was natürlich durch die Größe der Images entsteht, und ich weis das man es mit einem MemoryCache beheben kann. Habe mir dazu auf der Developer Seite auch das Training angeschaut.
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Steige aber trotzdem nicht ganz dahinter. Nun frage ich euch ob ihr mir da weiterhelfen könnt bzw. schonmal mit einem MemoryCache gearbeitet habt.
Müsste doch irgendwie möglich sein die Bilder schon mit dem AppStart in den Cache zu laden ohne großen Aufwand.
Bin für jeden Tipp sehr dankbar.

Euer Dee

Antworten
deeprojects
  • Forum-Beiträge: 35

04.01.2014, 22:36:48 via App

Habe Probleme es auf meine App anzuwenden. Weis nicht wie ich die Bilder die ich brauche ganz am Anfang in den Cache lade.

Antworten
deeprojects
  • Forum-Beiträge: 35

04.01.2014, 23:54:02 via Website

werde ich gleich mal testen danke ;)

Antworten
deeprojects
  • Forum-Beiträge: 35

05.01.2014, 15:20:37 via Website

Habe es nochmal getestet, mich aber dann doch für das Google Training Tutorial entschieden, scheint auch i,wie abzuspeichern aber wenn ich sie laden will zeigt er mir keine Images mehr an.

Hier mal mein Code:
1package de.uulm.quartett;
2
3import android.app.ActivityOptions;
4import android.content.Intent;
5import android.content.res.Resources;
6import android.graphics.Bitmap;
7import android.graphics.BitmapFactory;
8import android.os.AsyncTask;
9import android.os.Bundle;
10import android.support.v4.app.FragmentActivity;
11import android.support.v4.util.LruCache;
12import android.support.v4.view.ViewPager;
13import android.util.Log;
14import android.view.View;
15import android.view.View.OnClickListener;
16import android.view.animation.Animation;
17import android.view.animation.AnimationUtils;
18import android.widget.ImageButton;
19import android.widget.ImageView;
20import android.widget.Toast;
21
22public class MainActivity extends FragmentActivity {
23 Animation animTranslate;
24 private GameSelectPagerAdapter mAdapter;
25 private ViewPager modes;
26 private LruCache<String, Bitmap> mMemoryCache;
27
28 ImageButton btn_startGame, btn_highScore, btn_decks, btn_profile, btn_info;
29
30 @Override
31 protected void onCreate(Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
33 setContentView(R.layout.activity_main);
34
35 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
36 Log.e("MAXMEMORY ", "" + maxMemory);
37
38 final int cacheSize = maxMemory / 8;
39
40 mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
41 @Override
42 protected int sizeOf(String key, Bitmap bitmap) {
43 // The cache size will be measured in kilobytes rather than
44 // number of items.
45 return bitmap.getByteCount() / 1024;
46 }
47 };
48
49 btn_startGame = (ImageButton) findViewById(R.id.imgBtnStartGame);
50 btn_highScore = (ImageButton) findViewById(R.id.imgBtnHighScore);
51 btn_decks = (ImageButton) findViewById(R.id.imgBtnDecks);
52 btn_profile = (ImageButton) findViewById(R.id.imgBtnProfile);
53 btn_info = (ImageButton) findViewById(R.id.imgBtn_info);
54
55 loadBitmap(R.drawable.slice_btn_new_game, btn_startGame);
56 loadBitmap(R.drawable.slice_btn_highscore, btn_highScore);
57 loadBitmap(R.drawable.slice_btn_decks, btn_decks);
58 loadBitmap(R.drawable.slice_btn_profil, btn_profile);
59
60 animTranslate = AnimationUtils.loadAnimation(this,
61 R.anim.anim_translate);
62 btn_startGame.setAnimation(animTranslate);
63 btn_startGame.setOnClickListener(new OnClickListener() {
64 @Override
65 public void onClick(View v) {
66 setContentView(R.layout.fragmentlayout);
67 mAdapter = new GameSelectPagerAdapter(
68 getSupportFragmentManager());
69 modes = (ViewPager) findViewById(R.id.pager);
70 modes.setAdapter(mAdapter);
71 }
72 });
73
74 btn_highScore.setAnimation(animTranslate);
75 btn_highScore.setOnClickListener(new OnClickListener() {
76 @Override
77 public void onClick(View v) {
78 // TODO Auto-generated method stub
79 Intent intent = new Intent(getApplicationContext(),
80 HighscoreActivity.class);
81 Bundle animation = ActivityOptions.makeCustomAnimation(
82 getApplicationContext(), R.anim.right_in,
83 R.anim.left_out).toBundle();
84 startActivity(intent, animation);
85 }
86 });
87
88 btn_decks.setAnimation(animTranslate);
89 btn_decks.setOnClickListener(new OnClickListener() {
90
91 @Override
92 public void onClick(View v) {
93 // TODO Auto-generated method stub
94 Intent intent = new Intent(getApplicationContext(),
95 DeckDetailActivity.class);
96 Bundle animation = ActivityOptions.makeCustomAnimation(
97 getApplicationContext(), R.anim.right_in,
98 R.anim.left_out).toBundle();
99 startActivity(intent, animation);
100 }
101 });
102
103 btn_profile.setAnimation(animTranslate);
104 btn_profile.setOnClickListener(new OnClickListener() {
105
106 @Override
107 public void onClick(View v) {
108 // TODO Auto-generated method stub
109 Intent intent = new Intent(getApplicationContext(),
110 ProfileActivity.class);
111 Bundle animation = ActivityOptions.makeCustomAnimation(
112 getApplicationContext(), R.anim.right_in,
113 R.anim.left_out).toBundle();
114 startActivity(intent, animation);
115 }
116 });
117
118 btn_info.setOnClickListener(new OnClickListener() {
119
120 @Override
121 public void onClick(View v) {
122 // TODO Auto-generated method stub
123 Toast.makeText(getApplicationContext(), "Infos folgen!",
124 Toast.LENGTH_LONG).show();
125 }
126 });
127 }
128
129 public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
130 if (getBitmapFromMemCache(key) == null) {
131 mMemoryCache.put(key, bitmap);
132 }
133 }
134
135 public Bitmap getBitmapFromMemCache(String key) {
136 return mMemoryCache.get(key);
137 }
138
139 public void loadBitmap(int resId, ImageView imageView) {
140 Log.e("STRING RES ID", "" + String.valueOf(resId));
141 final String imageKey = String.valueOf(resId);
142
143 final Bitmap bitmap = getBitmapFromMemCache(imageKey);
144 if (bitmap != null) {
145 imageView.setImageBitmap(bitmap);
146 } else {
147 imageView.setImageResource(R.drawable.slice_info);
148 BitmapWorkerTask task = new BitmapWorkerTask();
149 task.execute(resId);
150 }
151 }
152
153 class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
154 @Override
155 protected Bitmap doInBackground(Integer... params) {
156 final Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
157 params[0]);
158 addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
159 return bitmap;
160 }
161 }
162}

Vllt. fällt ja jemanden etwas auf wäre dankbar bin glaub vor lauter rumprobieren schon voll neben der Spur.

Antworten
deeprojects
  • Forum-Beiträge: 35

05.01.2014, 15:37:45 via Website

habe sie im drawable Ordner drin

Antworten
Ludy
  • Admin
  • Forum-Beiträge: 7.959

05.01.2014, 16:23:08 via Website

1package de.uulm.quartett;
2
3import android.app.ActivityOptions;
4import android.content.Intent;
5import android.content.res.Resources;
6import android.graphics.Bitmap;
7import android.graphics.BitmapFactory;
8import android.os.AsyncTask;
9import android.os.Bundle;
10import android.support.v4.app.FragmentActivity;
11import android.support.v4.util.LruCache;
12import android.support.v4.view.ViewPager;
13import android.util.Log;
14import android.view.View;
15import android.view.View.OnClickListener;
16import android.view.animation.Animation;
17import android.view.animation.AnimationUtils;
18import android.widget.ImageButton;
19import android.widget.ImageView;
20import android.widget.Toast;
21
22public class MainActivity extends FragmentActivity {
23 Animation animTranslate;
24 private GameSelectPagerAdapter mAdapter;
25 private ViewPager modes;
26 private LruCache<String, Bitmap> mMemoryCache;
27
28 ImageButton btn_startGame, btn_highScore, btn_decks, btn_profile, btn_info;
29
30 @Override
31 protected void onCreate(Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
33 setContentView(R.layout.activity_main);
34
35 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
36 Log.e("MAXMEMORY ", "" + maxMemory);
37
38 final int cacheSize = maxMemory / 8;
39
40 mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
41 @Override
42 protected int sizeOf(String key, Bitmap bitmap) {
43 // The cache size will be measured in kilobytes rather than
44 // number of items.
45 return bitmap.getByteCount() / 1024;
46 }
47 };
48
49 btn_startGame = (ImageButton) findViewById(R.id.imgBtnStartGame);
50 btn_highScore = (ImageButton) findViewById(R.id.imgBtnHighScore);
51 btn_decks = (ImageButton) findViewById(R.id.imgBtnDecks);
52 btn_profile = (ImageButton) findViewById(R.id.imgBtnProfile);
53 btn_info = (ImageButton) findViewById(R.id.imgBtn_info);
54
55 loadBitmap(R.drawable.slice_btn_new_game, btn_startGame);
56 loadBitmap(R.drawable.slice_btn_highscore, btn_highScore);
57 loadBitmap(R.drawable.slice_btn_decks, btn_decks);
58 loadBitmap(R.drawable.slice_btn_profil, btn_profile);
59
60 animTranslate = AnimationUtils.loadAnimation(this,
61 R.anim.anim_translate);
62 btn_startGame.setAnimation(animTranslate);
63 btn_startGame.setOnClickListener(new OnClickListener() {
64 @Override
65 public void onClick(View v) {
66 setContentView(R.layout.fragmentlayout);
67 mAdapter = new GameSelectPagerAdapter(
68 getSupportFragmentManager());
69 modes = (ViewPager) findViewById(R.id.pager);
70 modes.setAdapter(mAdapter);
71 }
72 });
73
74 btn_highScore.setAnimation(animTranslate);
75 btn_highScore.setOnClickListener(new OnClickListener() {
76 @Override
77 public void onClick(View v) {
78 // TODO Auto-generated method stub
79 Intent intent = new Intent(getApplicationContext(),
80 HighscoreActivity.class);
81 Bundle animation = ActivityOptions.makeCustomAnimation(
82 getApplicationContext(), R.anim.right_in,
83 R.anim.left_out).toBundle();
84 startActivity(intent, animation);
85 }
86 });
87
88 btn_decks.setAnimation(animTranslate);
89 btn_decks.setOnClickListener(new OnClickListener() {
90
91 @Override
92 public void onClick(View v) {
93 // TODO Auto-generated method stub
94 Intent intent = new Intent(getApplicationContext(),
95 DeckDetailActivity.class);
96 Bundle animation = ActivityOptions.makeCustomAnimation(
97 getApplicationContext(), R.anim.right_in,
98 R.anim.left_out).toBundle();
99 startActivity(intent, animation);
100 }
101 });
102
103 btn_profile.setAnimation(animTranslate);
104 btn_profile.setOnClickListener(new OnClickListener() {
105
106 @Override
107 public void onClick(View v) {
108 // TODO Auto-generated method stub
109 Intent intent = new Intent(getApplicationContext(),
110 ProfileActivity.class);
111 Bundle animation = ActivityOptions.makeCustomAnimation(
112 getApplicationContext(), R.anim.right_in,
113 R.anim.left_out).toBundle();
114 startActivity(intent, animation);
115 }
116 });
117
118 btn_info.setOnClickListener(new OnClickListener() {
119
120 @Override
121 public void onClick(View v) {
122 // TODO Auto-generated method stub
123 Toast.makeText(getApplicationContext(), "Infos folgen!",
124 Toast.LENGTH_LONG).show();
125 }
126 });
127 }
128
129 public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
130 if (getBitmapFromMemCache(key) == null) {
131 mMemoryCache.put(key, bitmap);
132 }
133 }
134
135 public Bitmap getBitmapFromMemCache(String key) {
136 return mMemoryCache.get(key);
137 }
138
139 public void loadBitmap(int resId, ImageView imageView) {
140 Log.e("STRING RES ID", "" + String.valueOf(resId));
141 final String imageKey = String.valueOf(resId);
142
143 final Bitmap bitmap = getBitmapFromMemCache(imageKey);
144 if (bitmap != null) {
145 imageView.setImageBitmap(bitmap);
146 } else {
147 imageView.setImageResource(R.drawable.slice_info);
148 BitmapWorkerTask task = new BitmapWorkerTask();
149 try {
150 imageView.setImageBitmap(task.execute(resId).get()); // Das hier
151 } catch (InterruptedException e) {
152 Log.e("InterruptedE", e.toString());
153 } catch (ExecutionException e) {
154 Log.e("Execution", e.toString());
155 }
156 }
157 }
158
159 class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
160 @Override
161 protected Bitmap doInBackground(Integer... params) {
162 final Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
163 params[0]);
164 addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
165 return bitmap;
166 }
167 }
168}


Probier das mal du hast vergessen das return vom AsyncTask auszulesen!

EDIT: In deinem Drawable Ordner ist nen Fehler erkennbar an dem Symbol

— geändert am 05.01.2014, 16:24:39

Gruß Ludy (App Entwickler)

Mein Beitrag hat dir geholfen? Lass doch ein "Danke" da.☺

☕ Buy Me A Coffee ☕

Lebensmittelwarnung-App

✨Meine Wunschliste✨

📲Telegram NextPit News📲

Antworten
Mac Systems
  • Forum-Beiträge: 1.727

05.01.2014, 17:02:19 via Website

@Ludy, er nutzt SVN oder GIT. Dateien wurde verändert und Eclipse zeigt das somit an. Dateien mit einem > vorweg wurden verändert

— geändert am 05.01.2014, 17:02:33

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten
deeprojects
  • Forum-Beiträge: 35

05.01.2014, 17:02:42 via Website

natürlich, danke für den tipp ;)

Antworten
deeprojects
  • Forum-Beiträge: 35

05.01.2014, 17:03:45 via Website

@MacSystems richtig ;)

— geändert am 05.01.2014, 17:04:03

Antworten
Ludy
  • Admin
  • Forum-Beiträge: 7.959

05.01.2014, 17:04:07 via App

Mac Systems
@Ludy, er nutzt SVN oder GIT. Dateien wurde verändert und Eclipse zeigt das somit an. Dateien mit einem > vorweg wurden verändert

Oh das wusste ich nicht hab mich schon gewunder warum da Pfeile sind.

Gruß Ludy (App Entwickler)

Mein Beitrag hat dir geholfen? Lass doch ein "Danke" da.☺

☕ Buy Me A Coffee ☕

Lebensmittelwarnung-App

✨Meine Wunschliste✨

📲Telegram NextPit News📲

Antworten