【问题标题】:How do I transfer an image from its URL to the SD card?如何将图像从其 URL 传输到 SD 卡?
【发布时间】:2012-11-27 21:55:15
【问题描述】:

如何将图像保存到从图像 URL 检索到的 SD 卡?

【问题讨论】:

  • @Akusete... 你应该在 output.write(buffer, 0, buffer.length); 中替换 'buffer.length'到字节读取。否则垃圾数据将附加在文件末尾。

标签: android image download android-sdcard


【解决方案1】:

首先,您必须确保您的应用程序具有写入 sdcard 的权限。为此,您需要在应用程序清单文件中添加使用权限write external storage。见Setting Android Permissions

然后您可以将 URL 下载到 sdcard 上的文件中。一个简单的方法是:

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();
}

编辑: 在清单中添加权限

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

【讨论】:

  • @Paresh:谢谢,我已更新代码以使用getExternalStorageDirectory()。你知道它是否返回一个斜杠吗?例如/sdcard/sdcard/
  • 您的问题没有实际意义,因为Environment.getExternalStorageDirectory() 不返回String,因此您的代码无法编译。我为你更正了你的代码。
  • aReasonableSize 是一个不会让您出现内存不足异常的大小。通常 1024 或 2048 就可以了。
【解决方案2】:

可以在 Android 开发者博客上的latest post 中找到一个很好的示例:

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;
}

【讨论】:

  • 这里不介绍如何将图片保存到sdcard,只介绍如何将图片下载到内存中。
猜你喜欢
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-26
相关资源
最近更新 更多