在我的活動中,我從url下載圖片。我只想第一次下載這些圖像。稍後當我訪問此頁面時,它應該從sdcard中獲取圖像。我怎樣才能做到這一點?誰能幫忙?如何將下載的文件存儲到SD卡然後檢索它?
清單中我已設置權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
,我使用的下載方法是:
public static Bitmap downloadFileFromUrl(String fileUrl){
URL myFileUrl =null;
Bitmap imageBitmap = null;
try {
myFileUrl= new URL(fileUrl);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection= (HttpURLConnection)myFileUrl.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream is = connection.getInputStream();
imageBitmap = BitmapFactory.decodeStream(is);
//Below two lines I just tried out for saving to sd card.
FileOutputStream out = new FileOutputStream(fileUrl);
imageBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
}
catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return imageBitmap;
}
u能用代碼解釋一下? – Mathew 2011-05-11 10:01:49