Come posso salvare un'immagine sulla scheda SD che richiamo dall'URL dell'immagine?Come trasferisco un'immagine dal suo URL alla scheda SD?
risposta
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" />
@Paresh: Grazie, ho aggiornato il codice per utilizzare 'getExternalStorageDirectory()'. Sai se restituisce una barra finale? per esempio. '/ sdcard' o'/sdcard/' – Akusete
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. –
cos'è aReasonableSize ?? –
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;
}
Questo non descrive come salvare l'immagine sulla sdcard, ma solo come scaricare l'immagine nella memoria. –
In che modo questa risposta ha ottenuto 9 upvotes?! ... –
- 1. Android scrittura alla cartella della scheda SD
- 2. Come riprodurre i video dalla scheda SD
- 3. Android: salva l'immagine dall'URL sulla scheda SD
- 4. Caricamento estraibile dalla scheda SD
- 5. Problemi con scheda SD Android
- 6. Come posso selezionare una scheda IE dal suo handle
- 7. Come ottenere una dimensione della scheda sd della memoria esterna (con scheda SD montata)?
- 8. Riavvia il servizio per l'app sulla scheda SD quando viene rimossa la scheda SD
- 9. Come posso scaricare un file video su una scheda SD?
- 10. Installazione di app Android senza scheda SD
- 11. Android KitKat 4.4 cartella su scheda SD
- 12. Android Come visualizzare l'anteprima di un'immagine, utilizzando il suo percorso file dalla scheda SD, dalla mia applicazione
- 13. Scansione scheda SD Android per nuovi file
- 14. Android Salva immagine su scheda SD
- 15. Permesso di scrivere sulla scheda SD
- 16. Android spostarsi sul comportamento della scheda SD
- 17. Mostra tutta la musica su scheda SD
- 18. Glide. Cache su memoria esterna (scheda SD)
- 19. Impossibile spostare un'app sulla scheda SD
- 20. Come accedere alla variabile di istanza dal suo "nome stringa"?
- 21. Come copiare le immagini sulla scheda SD dell'emulatore Android dal disco rigido?
- 22. Come rimuovere la scheda SD in modo programmatico su Android
- 23. Come posso ascoltare il montaggio della scheda SD in Android?
- 24. Come aprire la cartella della scheda SD in Android AVD?
- 25. Come ricevere le notifiche per gli eventi della scheda SD?
- 26. Come installare la scheda SD sull'emulatore Android (1.6)
- 27. Come rilevare se il dispositivo possiede una scheda SD?
- 28. Come caricare l'immagine dalla scheda SD utilizzando la libreria Picasso
- 29. Come aprire un PDF tramite Intent dalla scheda SD
- 30. Come posso leggere il file json dalla scheda SD
@Akusete ... dovresti sostituire "buffer.length" in output.write (buffer, 0, buffer.length); a byteLeggi. Altrimenti i dati spazzatura verranno aggiunti alla fine del file. – shaffooo