假设我有一组图像 URL,我会尝试使用 glide 一个一个下载所有这些图像。目前,当我提供 URL 时,我可以下载单个图像。这是代码:
private void downx()
{
File sd = getExternalCacheDir();
File folder = new File(sd, "/mobio/");
if (!folder.exists()) {
if (!folder.mkdir()) {
Log.e("ERROR", "Cannot create a directory!");
} else {
folder.mkdirs();
}
}
final File[] fileName = {new File(folder, "one.jpg"), new File(folder, "two.jpg"),new File(folder, "three.jpg")};
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params)
{
try
{
theBitmap = Glide.
with(getApplicationContext()).
load(urls[2]).
asBitmap().
into(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL).
get();
}
catch (final ExecutionException e)
{
Log.e("TAG", e.getMessage());
}
catch (final InterruptedException e)
{
Log.e("TAG", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void dummy) {
if (null != theBitmap) {
// The full bitmap should be available here
Log.d("TAG", "Image loaded");
Log.e("GLIDE","I am Ready");
try {
FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName[1]));
theBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.execute();
}
现在的问题是:如果我需要下载多个图像,我应该采用什么方法,以及如何强制我的代码适应处理多个下载?
最佳答案
我使用了一个类,该类获取 URL 列表并将图像下载到文件中。请检查这个Gist了解更多。这使用 Picasso 下载图像,但您也可以编辑下载代码以使用 glide。应该是一行更改。希望这会有所帮助。
https://gist.github.com/bpr10/a765a015bf1c774816ba58c7ae6413d6
关于java - 如何使用 Glide [Android] 下载多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41605505/