在我的应用程序中,系统将从 url 下载图像并将其保存到手机内存中。(我没有在问题中包含 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();
}
}
});