JPEG vom Server laden Fehler...

  • Antworten:2
Hans Meier
  • Forum-Beiträge: 12

07.10.2010, 15:20:06 via Website

Ich hole mir ein JPG von einer URL und möchte dieses als Drawable abspeichern. Leider wird das JPEG nicht komplett geladen bzw weißt Fehler auf?
Kennt jemand eine Möglichkeit wie man dies richtig macht?

Hier der relevante Code:

1private void createExternalStoragePrivateFile(String url, String path, int j) {
2 // Create a path where we will place our private file on external
3 // storage.
4
5 File file = new File(getExternalFilesDir(null), path);
6
7
8 if (!file.exists()) {
9
10 Log.i("CREATEEXTERNALSTORAGE", "URL: " + url);
11
12 try {
13 // Very simple code to copy a picture from the application's
14 // resource into the external file. Note that this code does
15 // no error checking, and assumes the picture is small (does not
16 // try to copy it in chunks). Note that if external storage is
17 // not currently mounted this will silently fail.
18 InputStream is = (InputStream) this.fetch(url);
19 OutputStream os = new FileOutputStream(file);
20 byte[] data = new byte[60000];
21 is.read(data);
22 os.write(data);
23
24 if (is != null) {
25 is.close();
26 }
27 if (os != null) {
28 os.close();
29 }
30
31 Drawable d = Drawable.createFromPath(file.getPath());
32 Log.i("PATH" , "P: " + file.getPath());
33 lOffer.get(j).setBildstandard(d);
34
35 } catch (IOException e) {
36 // Unable to create file, likely because external storage is
37 // not currently mounted.
38 Log.w("ExternalStorage", "Error writing " + file, e);
39 }
40 } else {
41 Log.i("INFO", "File already exists");
42 }
43
44 }


1public Object fetch(String address) throws MalformedURLException,IOException, FileNotFoundException {
2 URL url = new URL(address);
3 Object content = url.getContent();
4 return content;
5 }

Antworten
Markus Gu
  • Forum-Beiträge: 2.644

07.10.2010, 16:26:09 via Website

hallo

auf dem blog der android developer hat google vorgestellt, wie man sowas am besten macht.

guck dir das mal an

swordiApps Blog - Website

Antworten
Hans Meier
  • Forum-Beiträge: 12

08.10.2010, 12:50:05 via Website

Hab mir die Beispiele mal angeschaut.

Allerdings habe ich das Problem, dass die Bilder fehlerhaft gespeichert werden.
Habt ihr ein Beispiel, indem man sieht wie ein Bild von einer URL auf der Speicherkarte gespeichert wird?

Gruß

Antworten