2017-01-12 70 views
1

提供我有一個圖像URL陣列,我試圖下載所有這些圖像一個接一個使用滑翔。目前,我可以在提供URL時下載單個圖像。這裏是代碼:如何使用Glide [Android]下載多個圖像?

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(); 



} 

現在問題是:我該採取什麼樣的方法,如果我需要下載多個圖像,以及如何強制我的代碼,以適應處理多個下載?

回答

-2

我想這將是更好地使用下載管理器,因爲它會處理排隊,網絡可用性等