2010-12-18 10 views
1

Ich muss ein einzelnes Bild zur Zeit aus dem Internet herunterladen und dann auf der SD-Karte speichern. Wie mache ich es? Ich habe einen Versuch unternommen, aber wenn ich versuche, das heruntergeladene Bild anzuzeigen, wird die Meldung "Keine Vorschau verfügbar" angezeigt. Bitte beachten Sie meinen Code unten:Bild herunterladen (SSL) und auf SD-Karte in Android speichern?

public class ImgDownloader { 

private static final int IO_BUFFER_SIZE = 4 * 1024; 

public static final byte[] downloadImage(String imgURL) { 
    byte[] data = null; 
    try { 
    Log.v("Down", "1"); 
    InputStream in = null; 
    BufferedOutputStream out = null; 
    in = new BufferedInputStream(new URL(imgURL).openStream(), 8 * 1024); 
    Log.v("Down", "2"); 
    final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); 
    out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE); 
    copy(in, out); 
    out.flush(); 
    Log.v("Down", "3"); 
    data = dataStream.toByteArray(); 
    // bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 
    Log.v("Down", "4"); 
    } catch (Exception ex) { 
    ex.printStackTrace(); 
    // System.out.println("Exception in Image Downloader .." 
    // +ex.getMessage()); 
    } 
    return data; 
} 

private static void copy(InputStream in, OutputStream out) 
    throws IOException { 
    byte[] b = new byte[IO_BUFFER_SIZE]; 
    int read; 
    while ((read = in.read(b)) != -1) { 
    out.write(b, 0, read); 
    } 
} 

} 

Hinweis: ich habe laden Sie das Bild von der SSL-Verbindung.

Irgendwelche Ideen? Danke im Voraus.

Antwort

1

Sie können so etwas wie

try{ 
     URL url = new URL(downloadUrl); //you can write here any link 
     File file = new File(absolutePath); //Something like ("/sdcard/file.mp3") 

      //Create parent directory if it doesn't exists 
      if(!new File(file.getParent()).exists()) 
      { 
       System.out.println("Path is created " + new File(file.getParent()).mkdirs()); 
      } 
      file = new File(absolutePath); //Something like ("/sdcard/file.mp3") 
      file.createNewFile(); 
      /* Open a connection to that URL. */ 
      URLConnection ucon = url.openConnection(); 
      /* 
      * Define InputStreams to read from the URLConnection. 
      */ 
      InputStream is = ucon.getInputStream(); 
      /* 
      * Read bytes to the Buffer until there is nothing more to read(-1). 
      */ 
      FileOutputStream fos = new FileOutputStream(file); 
      int size = 1024*1024; 
      byte[] buf = new byte[size]; 
      int byteRead; 
      while (((byteRead = is.read(buf)) != -1)) { 
       fos.write(buf, 0, byteRead); 
       bytesDownloaded += byteRead; 
      } 
      /* Convert the Bytes read to a String. */ 

      fos.close(); 

    }catch(IOException io) 
    { 
     networkException = true; 
     continueRestore = false; 
    } 
    catch(Exception e) 
    { 
     continueRestore = false; 
     e.printStackTrace(); 
    } 

Nehmen Sie die entsprechenden Änderungen entsprechend Ihrer Anforderung versuchen. Ich benutze den gleichen Code zum Herunterladen von Dateien aus dem Internet und Speichern auf SDCard.

Hoffe es hilft !!

Verwandte Themen