Hans Meier
- Forum-Beiträge: 12
07.10.2010, 15:20:06 via Website
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:
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 }
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 }
2 URL url = new URL(address);
3 Object content = url.getContent();
4 return content;
5 }
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.