我正在创建一个可以从互联网上下载图片的应用程序!图像下载没有问题!但问题是,它们被下载到一个未知的文件夹中,并且这些下载的图像在图库应用程序中看不到!如何下载特定图库文件夹中的图像,使其在图库中也可见?提前感谢!
下载方法:
DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDescription("Downloading Wallpaper").setTitle("Downloading");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
myDownloadReference = dm.enqueue(request);
Toast.makeText(Wallpaper.this, "Downloading..", Toast.LENGTH_SHORT).show();
}
发布于 2017-07-25 20:16:38
位图的图像url
try {
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch(IOException e) {
System.out.println(e);
}
public void saveImageToExternal(String imgName, Bitmap bm) throws IOException {
//Create Path to save Image
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+appFolder); //Creates app specific folder
path.mkdirs();
File imageFile = new File(path, imgName+".png"); // Imagename.png
FileOutputStream out = new FileOutputStream(imageFile);
try{
bm.compress(Bitmap.CompressFormat.PNG, 100, out); // Compress Image
out.flush();
out.close();
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(context,new String[] { imageFile.getAbsolutePath() }, null,new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch(Exception e) {
throw new IOException();
}
}
发布于 2017-07-25 20:10:03
下载文件后尝试:
// refresh gallery
try {
MediaScannerConnection.scanFile(getActivity(), new String[]{savedImagePath}, null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
// ApplicationUtil.showToast(getActivity(), "onScanCompleted!");
}
});
} catch (Exception e) {
}
这将刷新您的图库。
发布于 2017-07-25 20:31:23
请查看以下链接:
要设置下载文件的位置,请执行以下操作:
https://developer.android.com/reference/android/app/DownloadManager.Request.html#setDestinationUri(android.net.Uri)(https://developer.android.com/reference/android/app/DownloadManager.Request.html#setDestinationUri(android.net.Uri%29)
http://www.programcreek.com/java-api-examples/index.php?class=android.app.DownloadManager.Request&method=setDestinationUri
要使下载的文件可由MediaScanner扫描,请执行以下操作:
https://developer.android.com/reference/android/app/DownloadManager.Request.html#allowScanningByMediaScanner()(https://developer.android.com/reference/android/app/DownloadManager.Request.html#allowScanningByMediaScanner(%29)
https://stackoverflow.com/questions/45302805
复制相似问题