- Forum-Beiträge: 12
15.11.2010, 14:49:54 via Website
15.11.2010 14:49:54 via Website
Ich lade Bilder von einem Webserver und speicher sie auf einer SD-Karte.
Bei einer kleinen Anzahl von Bilder funktioniert das wunderbar, ab ca 50. Bildern bekomme ich einen OutOfMemoryError
Die Bilder haben eine Auflösung von 313x313 und sind in der Regel zwischen 15KB - 60KB groß.
Ich verstehe nicht, warum der Speicher überlauft?!?
Vielleicht hatte jemand schonmal so eine Situation...
URL = URL des Bildes
Path = der Pfad mit dem es auf der SD-Karte gespeichert wird
Source = von welcher Funktion der Aufruf kommt (optional)
Quality = Qualität beim erstellen des Bildes auf der SD-Karte (bm.compress)
2
3 File file = null;
4 Bitmap bm = null;
5 Drawable d = null;
6
7
8 try {
9 file = new File(RealApplication.externalFilesDir, path);
10
11 } catch (NullPointerException e) {
12 e.printStackTrace();
13 Log.e("NULLPOINTEREXCEPTION", "new File");
14 return null;
15 }
16
17 if (!file.exists()) {
18
19 try {
20
21 Options options = new Options();
22 options.inPurgeable = true;
23
24 HttpGet httpRequest = null;
25 httpRequest = new HttpGet(url);
26 HttpClient httpclient = new DefaultHttpClient();
27 HttpResponse response = (HttpResponse) httpclient
28 .execute(httpRequest);
29 HttpEntity entity = response.getEntity();
30 BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
31 InputStream instream = bufHttpEntity.getContent();
32 bm = BitmapFactory.decodeStream(instream, null, options);
33 OutputStream os = new FileOutputStream(file);
34
35
36
37 bm.compress(CompressFormat.JPEG, quality, os);
38
39 bm.recycle();
40 d = Drawable.createFromPath(file.getPath());
41 os.close();
42 instream.close();
43
44 } catch (IOException e) {
45 // Unable to create file, likely because external storage is
46 // not currently mounted.
47 Log.e("ExternalStorage", "Error writing " + file, e);
48 } catch (NullPointerException e) {
49 Log.e("NULLPOINTEREXCEPTION", "in createExternal");
50 } catch (IllegalArgumentException e) {
51 Log.e("ILLEGALARGUMENTEXCEPTION", "in createExternal");
52 }
53
54
55 } else {
56 Log.i("INFO", "File already exists");
57 d = Drawable.createFromPath(file.getPath());
58
59 }
60
61 return d;
62
63 }
211-15 14:34:58.682: ERROR/dalvikvm(4927): Out of memory: Heap Size=11143KB, Allocated=2908KB, Bitmap Size=13357KB
311-15 14:34:58.682: ERROR/GraphicsJNI(4927): VM won't let us allocate 195938 bytes
411-15 14:34:58.682: DEBUG/skia(4927): --- decoder->decode returned false
511-15 14:34:58.682: DEBUG/AndroidRuntime(4927): Shutting down VM
611-15 14:34:58.682: WARN/dalvikvm(4927): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
711-15 14:34:58.691: ERROR/AndroidRuntime(4927): FATAL EXCEPTION: main
811-15 14:34:58.691: ERROR/AndroidRuntime(4927): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
911-15 14:34:58.691: ERROR/AndroidRuntime(4927): at android.graphics.BitmapFactory.nativeDecodeFile(Native Method)
1011-15 14:34:58.691: ERROR/AndroidRuntime(4927): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:287)
1111-15 14:34:58.691: ERROR/AndroidRuntime(4927): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:305)
1211-15 14:34:58.691: ERROR/AndroidRuntime(4927): at android.graphics.drawable.Drawable.createFromPath(Drawable.java:801)
— geändert am 15.11.2010, 14:51:19
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.