2010-07-21 9 views

risposta

46

Per prima cosa è necessario assicurarsi che l'applicazione disponga dell'autorizzazione per scrivere sulla sdcard. Per fare questo è necessario aggiungere il permesso di utilizzo scrivere la memoria esterna nel file manifest delle applicazioni. Vedi Setting Android Permissions

Quindi è possibile scaricare l'URL su un file sulla sdcard. Un modo semplice è:

URL url = new URL ("file://some/path/anImage.png"); 
InputStream input = url.openStream(); 
try { 
    //The sdcard directory e.g. '/sdcard' can be used directly, or 
    //more safely abstracted with getExternalStorageDirectory() 
    File storagePath = Environment.getExternalStorageDirectory(); 
    OutputStream output = new FileOutputStream (new File(storagePath,"myImage.png")); 
    try { 
     byte[] buffer = new byte[aReasonableSize]; 
     int bytesRead = 0; 
     while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { 
      output.write(buffer, 0, bytesRead); 
     } 
    } finally { 
     output.close(); 
    } 
} finally { 
    input.close(); 
} 

EDIT: Put autorizzazione manifesta

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+1

@Paresh: Grazie, ho aggiornato il codice per utilizzare 'getExternalStorageDirectory()'. Sai se restituisce una barra finale? per esempio. '/ sdcard' o'/sdcard/' – Akusete

+1

La tua domanda è discutibile perché' Environment.getExternalStorageDirectory() 'non restituisce un' String' e pertanto il tuo codice non viene compilato. Ho corretto il tuo codice per te. –

+3

cos'è aReasonableSize ?? –

8

Un ottimo esempio può essere trovato nel latest post sul blog degli sviluppatori Android:

static Bitmap downloadBitmap(String url) { 
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); 
    final HttpGet getRequest = new HttpGet(url); 

    try { 
     HttpResponse response = client.execute(getRequest); 
     final int statusCode = response.getStatusLine().getStatusCode(); 
     if (statusCode != HttpStatus.SC_OK) { 
      Log.w("ImageDownloader", "Error " + statusCode + 
       " while retrieving bitmap from " + url); 
      return null; 
     } 

     final HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream inputStream = null; 
      try { 
       inputStream = entity.getContent(); 
       final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
       return bitmap; 
      } finally { 
       if (inputStream != null) { 
        inputStream.close(); 
       } 
       entity.consumeContent(); 
      } 
     } 
    } catch (Exception e) { 
     // Could provide a more explicit error message for IOException or 
     // IllegalStateException 
     getRequest.abort(); 
     Log.w("ImageDownloader", "Error while retrieving bitmap from " + url, 
      e.toString()); 
    } finally { 
     if (client != null) { 
      client.close(); 
     } 
    } 
    return null; 
} 
+4

Questo non descrive come salvare l'immagine sulla sdcard, ma solo come scaricare l'immagine nella memoria. –

+1

In che modo questa risposta ha ottenuto 9 upvotes?! ... –

Problemi correlati