2010-07-21 15 views

Odpowiedz

46

Najpierw upewnij się, że aplikacja ma uprawnienia do zapisu na sdcard. Aby to zrobić, musisz dodać uprawnienia do używania zapisać pamięć zewnętrzną w pliku manifestu aplikacji. Zobacz: Setting Android Permissions

Następnie możesz pobrać adres URL do pliku na sdcard. Prostym sposobem jest:

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: pozwolenie Put w jawnym

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

@Paresh: Dzięki, zaktualizowałem kod, aby użyć 'getExternalStorageDirectory()'. Czy wiesz, czy zwraca on ukośnik? na przykład '/ sdcard' lub'/sdcard/' – Akusete

+1

Twoje pytanie jest dyskusyjne, ponieważ' Environment.getExternalStorageDirectory() 'nie zwraca' String', a zatem twój kod się nie kompiluje. Poprawiłem dla ciebie twój kod. –

+3

co to jest aReasonableSize? –

8

Doskonałym przykładem może być znaleziony w latest post na blogu Android dewelopera:

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

Nie opisuje to sposobu zapisania obrazu na karcie SD, a jedynie pobierania obrazu do pamięci. –

+1

W jaki sposób ta odpowiedź dostała 9 przegranych?! ... –

Powiązane problemy