gpt4 book ai didi

android - 将图像从 URL 保存在 Android 中的特定位置

转载 作者:行者123 更新时间:2023-11-29 16:55:13 28 4
gpt4 key购买 nike

我正在创建一个从互联网上下载图像的应用程序!图片下载没有问题!但问题是它们被下载到一个未知的文件夹中,而这些下载的图像在图库应用程序中是看不到的!如何将图片下载到特定图库文件夹中,以便它们在图库中也可见? 提前致谢!

下载方法:

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

最佳答案

位图的图片地址

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

关于android - 将图像从 URL 保存在 Android 中的特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45302805/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com