大佬教程收集整理的这篇文章主要介绍了从 URL android 下载在 imageview 中加载的图像,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 image@H_607_4@ 中加载了
image vIEw@H_607_4@ 的
URL@H_607_4@。我想下载图像并将其保存到外部存储。
这里我使用 GlIDe@H_607_4@ 从
image@H_607_4@ 加载
URL@H_607_4@。
GlIDe.with( this )
.load( url )
.placeholder(R.drawable.placeholder) // any placeholder to load at start
.error(R.drawable.imagenotfound) // any image in case of error
.diskCacheStrategy( diskCacheStrategy.ALL )
.into(meme_Image_vIEw);
@H_607_4@
接下来,当用户点击下载按钮时,我想将 image@H_607_4@ 中加载的
image vIEw@H_607_4@ 下载到外部存储。
这里是我使用的代码:
download_btn.setonClickListener(new VIEw.onClickListener() {
public voID onClick(VIEw v) {
if(checkPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE,STORAGE_PERMISSION_CODE)){
BitmapDrawable draw = (BitmapDrawablE) meme_Image_vIEw.getDrawable();
Bitmap bitmap = draw.getBitmap();
fiLeoutputStream outStream = null;
file sdCard = Environment.getExternalStorageDirectory();
file dir = new file(sdCard.getabsolutePath() + "/TirunelveliParithabangal");
dir.mkdirs();
String filename = String.format("%d.jpg",System.currentTimeMillis());
file outfile = new file(dir,fileName);
try {
outStream = new fiLeoutputStream(outfilE);
} catch (fileNotFoundException E) {
e.printstacktrace();
}
bitmap.compress(Bitmap.CompressFormat.JPEG,100,outStream);
try {
outStream.flush();
} catch (IOException E) {
e.printstacktrace();
}
try {
outStream.close();
} catch (IOException E) {
e.printstacktrace();
}
TastyToast.makeText(getApplicationContext(),"Meme Downloaded successfully",TastyToast.LENGTH_LONG*6,TastyToast.succesS);
}
else {
TastyToast.makeText(getApplicationContext(),"Please Grant Permission",TastyToast.succesS);
}
}
});
@H_607_4@
这里是权限检查功能:
// Function to check and request permission.
public Boolean checkPermission(final String permission,final int requestCodE)
{
if (ContextCompat.checkSelfPermission(ImageVIEwActivity.this,permission)
== PackageManager.PERMISSION_DENIED) {
Pop.on(this).with().layout(R.layout.permission_alert_info).when(new Pop.Yah() {
@OverrIDe
public voID clicked(DialogInterface dialog,View view) {
// requesTing the permission
ActivityCompat.requestPermissions(ImageVIEwActivity.this,new String[] { permission },requestCodE);
}
}).show();
}
else {
return true;
}
return false;
}
@H_607_4@
单击下载按钮时,我收到 file notFound 错误。
W/System.err: java.io.fileNotFoundException: /storage/emulated/0/TirunelveliParithabangal/1619611629915.jpg: open Failed: ENOENT (No such file or directory)
@H_607_4@
我不知道如何解决这个问题。请帮我解决一些问题。
尝试使用它从 URL 下载图像,它对我的代码有很大帮助,即使您在通知栏上获得了下载。调用时可以设置文件名
public static void downloadImage(Context context,String filename,String downloadUrlOfImagE){
try{
DownloadManager dm = (DownloadManager) context.getSystemservice(Context.DOWNLOAD_serviCE);
Uri downloadUri = Uri.parse(downloadUrlOfImagE);
DownloadManager.request request = new DownloadManager.request(downloadUri);
request.setAllowedNetworkTypes(DownloadManager.request.NETWORK_WIFI | DownloadManager.request.NETWORK_MOBILE)
.setAllowedOverroaR_491_11845@ing(false)
.settitle("result_"+fileName)
.setMimeType("image/jpeg") // Your file type. You can use this code to download other file types also.
.setNotificationVisibility(DownloadManager.request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDesTinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,File.separator+"TirunelveliParithabangal"+File.separator + filename + ".jpg");
dm.enqueue(request);
Log.d(tag,"Image download started");
}catch (Exception E){
Log.d(tag,"Image download failed.");
Toast.makeText(context,"Image download failed: "+filename,Toast.LENGTH_SHORT).show();
}
}
@H_607_4@
,
如果你想使用一个库而不是
dependencies {
implementation 'com.github.artjimlop:altex-image-downloader:'
}
@H_607_4@
并使用此代码下载
AltexImageDownloader.writeToDisk(context,PICTURE_URL,NAME_FOLDER); //NAME_FOLDER is the name of the folder where you want to save the image.
@H_607_4@
只需获取网址并将其替换为 PICTURE_URL@H_607_4@
以上是大佬教程为你收集整理的从 URL android 下载在 imageview 中加载的图像全部内容,希望文章能够帮你解决从 URL android 下载在 imageview 中加载的图像所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。