[英]How to download multiple images using Glide [Android]?
如果我有一個圖像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列表並將圖像下載到文件的類。 請檢查此要點以獲取更多信息。 這使用Picasso下載圖像,但是您也可以編輯下載代碼以使用glide。 應該是一行更改。 希望這可以幫助。
https://gist.github.com/bpr10/a765a015bf1c774816ba58c7ae6413d6
我想最好使用Download Manager,因為它可以處理排隊,網絡可用性等問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.