在我的應用程序,系統會從網址下載圖片並保存到手機內存。 (我沒有包括在這個問題的URL地址)最重要的是,它也將數據保存到SQLite數據庫。保存的數據是文件名,文件路徑和文件大小。但是目前一旦我經歷了下載過程,無論下載完成還是在進程中失敗,它也會插入到數據庫中。
有沒有我可以檢查下載過程是否完成沒有什麼辦法?如何檢查下載鏡像的過程是否完成?
GalleryScreen.this.runOnUiThread(new Runnable() {
@Override
public void run() {
boolean isDownloadResult = false;
int NumIncrease = 0;
Log.i(TAG, "NumberIncrease:" +NumIncrease);
Toast.makeText(getApplicationContext(), "Downloading.............>>>>>>>>>>>", Toast.LENGTH_SHORT).show();
Bitmap bm;
InputStream in;
try{
in = new java.net.URL(URL).openStream();
bm = BitmapFactory.decodeStream(new PatchInputStream(in));
File storage = new File(Environment.getExternalStorageDirectory() + File.separator + "/Images/");
Log.i(TAG,"storage:" +storage);
Log.i(TAG,"storage:" +storage.getAbsolutePath());
if(!storage.exists()){
storage.mkdirs();
}
String FileName = "/"+CONTENT_ID+".jpg";
FileOutputStream fos = new FileOutputStream(storage + FileName);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fos);
String filepath = storage + FileName;
File filecheck = new File (filepath);
long fileSize = filecheck.length();
fos.flush();
fos.close();
Log.i(TAG, "bm:" +bm);
Log.i(TAG, "fos:" +fos);
Log.i(TAG, "filesize:" +fileSize);
Log.i(TAG, "filepath:" +filepath);
helper.insert_content(filepath, fileSize, requestTime);
isDownload = false;
}
catch(IOException e1){
e1.printStackTrace();
}
}
});
如果你使用的AsyncTask那麼你可以檢查它是否正在運行或不 – rajpara