2010-07-21 39 views

Trả lời

46

Trước tiên, bạn phải đảm bảo rằng ứng dụng của bạn có quyền ghi vào sdcard. Để thực hiện việc này, bạn cần thêm quyền sử dụng ghi bộ nhớ ngoài vào tệp kê khai ứng dụng của bạn. Xem Setting Android Permissions

Sau đó, bạn có thể tải URL xuống tệp trên sdcard. Một cách đơn giản là:

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: phép Đặt trong manifest

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

@Paresh: Cảm ơn, tôi đã cập nhật mã để sử dụng 'getExternalStorageDirectory()'. Bạn có biết nếu nó trả về dấu gạch chéo không? ví dụ. '/ sdcard' hoặc'/sdcard/' – Akusete

+1

Câu hỏi của bạn là tranh luận vì' Environment.getExternalStorageDirectory() 'không trả về một' Chuỗi' và do đó mã của bạn không biên dịch. Tôi đã sửa mã của bạn cho bạn. –

+3

aReasonableSize ?? –

8

Một ví dụ tuyệt vời có thể được tìm thấy trong latest post trên blog Android phát triển của:

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

Điều này không mô tả cách lưu hình ảnh vào sdcard, chỉ cách tải hình ảnh vào bộ nhớ. –

+1

Câu trả lời này nhận được 9 upvotes như thế nào?! ... –

Các vấn đề liên quan