我正在尝试向我的应用添加下载功能,我正在使用Glide Library从他们的URL加载图片,使用下面的代码,我可以将图片保存到设备存储中,但它没有按我预期的那样工作。
new AsyncTask<FutureTarget<Bitmap>, Void, Bitmap>() {
@Override protected Bitmap doInBackground(FutureTarget<Bitmap>... params) {
try {
return params[0].get();
} catch (Exception ex) {
return null;
}
}
@Override protected void onPostExecute(Bitmap bitmap) {
if(bitmap == null) return;
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Art_"+System.currentTimeMillis());
}
}.execute(Glide.with(getApplicationContext()).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(width, height));
谁能帮我解决这个问题?
最佳答案
更新
1. list 文件中的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- 需要协程做一些后台工作
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
- 滑动以获取位图形式的图像
CoroutineScope(Dispatchers.IO).launch {
saveImage(Glide.with(this@MainActivity)
.asBitmap()
.load("https://i.imgur.com/4HFRb2z.jpg") // sample image
.placeholder(android.R.drawable.progress_indeterminate_horizontal) // need placeholder to avoid issue like glide annotations
.error(android.R.drawable.stat_notify_error) // need error to avoid issue like glide annotations
.submit()
.get())
}
- 将图像保存在新创建的具有特定名称的文件夹中
private fun saveImage(image: Bitmap): String? {
var savedImagePath: String? = null
val imageFileName = "JPEG_" + "FILE_NAME" + ".jpg"
val storageDir = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
.toString() + "/YOUR_FOLDER_NAME"
)
var success = true
if (!storageDir.exists()) {
success = storageDir.mkdirs()
}
if (success) {
val imageFile = File(storageDir, imageFileName)
savedImagePath = imageFile.getAbsolutePath()
try {
val fOut: OutputStream = FileOutputStream(imageFile)
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut)
fOut.close()
} catch (e: Exception) {
e.printStackTrace()
}
// Add the image to the system gallery
galleryAddPic(savedImagePath)
//Toast.makeText(this, "IMAGE SAVED", Toast.LENGTH_LONG).show() // to make this working, need to manage coroutine, as this execution is something off the main thread
}
return savedImagePath
}
- 将图像添加到画廊并发送广播(不提太多,因为这是不可能的)
private fun galleryAddPic(imagePath: String?) {
imagePath?.let { path ->
val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
val f = File(path)
val contentUri: Uri = Uri.fromFile(f)
mediaScanIntent.data = contentUri
sendBroadcast(mediaScanIntent)
}
}
原创
1. list 文件中的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2.滑动下载图片为位图
Glide.with(mContext)
.load("YOUR_URL")
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
saveImage(resource);
}
});
- 将位图保存到你的内存中
private String saveImage(Bitmap image) {
String savedImagePath = null;
String imageFileName = "JPEG_" + "FILE_NAME" + ".jpg";
File storageDir = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/YOUR_FOLDER_NAME");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
// Add the image to the system gallery
galleryAddPic(savedImagePath);
Toast.makeText(mContext, "IMAGE SAVED", Toast.LENGTH_LONG).show();
}
return savedImagePath;
}
private void galleryAddPic(String imagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
关于android - 使用 Glide 将图片保存到存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44761720/