如何通过http将远程图像复制到android中的gallery文件夹?

时间:2014-11-23 11:07:14

标签: android android-image android-gallery

我想要复制远程图像,例如" http://example.com/example.jpg"到Android用户手机内置的库...我该怎么办?

2 个答案:

答案 0 :(得分:0)

为此,您应该下载图像并将其保存在内部存储器中。

您可以自己下载图片:

public static Bitmap getBitmap(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Bitmap d = BitmapFactory.decodeStream(is);
        is.close();
        return d;
    } catch (Exception e) {
        return null;
    }
}

Code from here但是大图像会出现内存问题。我强烈建议您使用Android Universal Image LoaderPicasso from square

等构建库

答案 1 :(得分:0)

您可以在这里找到example of how to use the Android DownloadManager to download your file

可以使用Environment类中定义的容量来确定目标路径。 Constant DIRECTORY_DCIM指向父目录,在该目录下,所有活动都可以创建存储其图像的自定义文件夹。您可以将自己的子文件夹设为目标文件夹

当您的图片完成下载后,您会注意到它不会列在默认的图库应用程序中,这是因为Android会为所有媒体文件构建索引,但仍然不知道您新下载的图像。每次启动Android设备时都会更新此索引,但由于每次添加文件时重启设备都不合适,您还可以通过代码方式通知索引服务创建新文件并需要使用索引编制索引这段代码:

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);

扫描文件后也应进行此扫描。