【发布时间】:2019-03-12 10:30:41
【问题描述】:
我有 recyclerview 并且在每个 recyclerview 项目中,我都有图像。我在每个 recyclerview 项目中也有一个下载按钮。
如果用户单击下载按钮,该 recyclerview 项目的图像将下载并存储。
到目前为止一切正常,现在的问题是,如果用户正在下载第一个产品的图像,并且用户将为下一个产品下载另一个图像,则图库中的图像将被替换。
public class DownloadImage extends AsyncTask<String, Void, Void> {
private Context context;
private DownloadImage(Context context) {
this.context = context;
}
@Override
protected Void doInBackground(String... params) {
int ii = 0;
for (int k=0;k<resellerProductLists.get(imgpos).getProductImageDtoList().size();k++) {
//Toast.makeText(context,resellerProductLists.get(0).getProductImageDtoList().get(k).getImageUrl(),Toast.LENGTH_SHORT).show();
Log.e("Image",resellerProductLists.get(imgpos).getProductImageDtoList().get(k).getImageUrl());
ii++;
String img = "IMG-";
String mPath = Environment.getExternalStorageDirectory().toString() + "/Temp/" + img + ii + ".jpg";
File Directory = new File(Environment.getExternalStorageDirectory().toString() + "/Temp");
Directory.mkdirs();
try {
File file = Glide.with(context)
.load(resellerProductLists.get(imgpos).getProductImageDtoList().get(k).getImageUrl())
.downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();
File imageFile = new File(mPath);
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(new FileOutputStream(mPath));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.flush();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
} catch (InterruptedException | ExecutionException | IOException e) {
hideLoading();
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void res) {
super.onPostExecute(res);
//prodImageUri1.addAll(prodImageUri);
// hideLoading();
Toast.makeText(context,"Download Complete Successfully", Toast.LENGTH_SHORT).show();
}
}
谁能帮我解决这个问题?
【问题讨论】:
-
图像替换,因为图像的名称每次都相同。您需要存储具有唯一名称的图像,以便您可以使用当前时间。
标签: android image file android-internal-storage