2016-01-22 71 views
0

我的目標是從給定的網址下載多個圖像並將它們保存到內存中。從url加載圖片到內部存儲

private void downloadImages(List<String> filename,JSONObject c) throws Exception{ 
    String fileName = c.getString("name") + ".json"; 
    String data = ""; 
    File images = getDir("Images", Context.MODE_PRIVATE); 
    File dir = new File(images,fileName); 
    System.out.println(dir.getAbsolutePath()); 
    List<Bitmap> pictures = new ArrayList<>(); 
    for(String s : filename){ 

    } 
} 

List<String> filename包含圖像的所有網址。 所以我想我需要做的是,下載圖像的每個網址在filename 的結構是這樣的:

Images 
------fileName 
--------------Image1 
--------------Image2 
------fileName2 
--------------Image1 
--------------Image2 

我想用這樣的:

AsyncHttpClient client = new AsyncHttpClient(); 
client.get("https://example.com/file.png", new FileAsyncHttpResponseHandler(/* Context */ this) { 
    @Override 
    public void onSuccess(int statusCode, Header[] headers, File response) { 
     // Do something with the file `response` 
    } 
}); 
+0

你的問題是什麼?你制定的方法不能按預期工作嗎?你試過了嗎? – vdelricco

+0

我的問題是,我必須在foreach中將下載的圖像保存在我想要的結構中。這意味着 - >使用JSONObject的名稱創建子文件夾,並在那裏存儲每個圖像。 – greedsin

回答

0

你需要看圖像還是使用圖像?如果你只是想使用圖像,然後重新發明輪子是沒有辦法去嘗試使用圖書館,而不是執行自己的事情,它會節省你很多的壓力和時間嘗試PicassoUniversal Image Loader所有的東西都是爲你做的。

+0

我將需要他們後 – greedsin

+0

這些圖像緩存在本地存儲只是遵循文檔 – christoandrew

3

試試這個令人敬畏的lib提供的facebook Fresco強大的圖像緩存和下載。