通用圖像裝載機庫的主要思想是圖像的異步下載和顯示他們內部查看。兌現是圖書館的功能之一。如果您需要緩存圖像而不顯示它們,則不應使用通用圖像加載程序。只需編寫一個簡單的AsyncTask類,即可將圖像下載到磁盤。 下面是下載圖像的函數示例,只需在您的AsyncTask的doInBackGround中爲要下載的所有圖像調用它即可。
private void downloadImagesToSdCard(String downloadUrl,String imageName)
{
try
{
URL url = new URL(downloadUrl); //you can write here any link
File myDir = new File("/sdcard"+"/"+Constants.imageFolder);
//Something like ("/sdcard/file.mp3")
if(!myDir.exists()){
myDir.mkdir();
Log.v("", "inside mkdir");
}
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = imageName;
File file = new File (myDir, fname);
if (file.exists()) file.delete();
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
InputStream inputStream = null;
HttpURLConnection httpConn = (HttpURLConnection)ucon;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
/*
* Define InputStreams to read from the URLConnection.
*/
// InputStream is = ucon.getInputStream();
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
FileOutputStream fos = new FileOutputStream(file);
int size = 1024*1024;
byte[] buf = new byte[size];
int byteRead;
while (((byteRead = inputStream.read(buf)) != -1)) {
fos.write(buf, 0, byteRead);
bytesDownloaded += byteRead;
}
/* Convert the Bytes read to a String. */
fos.close();
}catch(IOException io)
{
networkException = true;
continueRestore = false;
}
catch(Exception e)
{
continueRestore = false;
e.printStackTrace();
}
}
你可以編寫你自己的類,只需創建一個內存緩存類,該類包含Map中的字符串id和位圖,只需放置並獲取操作即可。 fileCache的另一個類,它將在緩存目錄上創建一個文件。然後只需寫入裝載機類,它將從網上下載圖像並在磁盤上執行它們。 – NaserShaikh