与AsyncTask的下载图片 [英] Download images with AsyncTask
问题描述
我真的不知道哪里出了问题我的code或结构。我想用的AsyncTask下载图片,并显示出在平均时间进度条。但我尝试了做几个不同的方式。它仍然失败,不知道什么地方错了。我的结构流量
I'm not really sure what goes wrong with my code or structure. I wanted to use AsyncTask to download images and display out the progress bar at the mean time. But I tried out a few different way of doing it. It still failed and no idea what's wrong with it. My structure flow is
内容ID是一个字符串数组存储图像的内容ID。
主要问题:它成功地从URL下载图像并存储到手机,但下载的图像都是相同的图像。它应该是不同的图像,这不是我所期待的。
次级问题:进度条弹出,而应用程序下载图像,但是进度条没有更新的进展。它只是保持为0%,并驳回了下载完成后。的
我想知道是什么原因,我提到的小学和副屏的问题。请发表评论或答案,如果你会知道什么是错我的code。任何帮助将AP preciated。
I wanted to know what causes primary and secodary issue as i mentioned. Please leave a comment or answer if you might know what's wrong with my code. Any help will be appreciated.
if(isSyncSuccess){
SetConstant.IMAGE_EXIST = 1;
pDialog = new ProgressDialog(GalleryScreen.this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setProgress(0);
pDialog.setMax(contentId.length);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
if (contentId.length>0){
Log.i(TAG, "contentid.length:" +contentId.length);
for (int i=0;i<contentId.length;i++){
if(helper.databaseChecking(useremail, contentId[i])){
contentdownload = i;
SetConstant.CONTENT_ID = contentId[i];
String URL = SetConstant.URL_DOWNLOAD_CONTENT+contentId[i];
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute(URL);
}
private class DownloadFile extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... sUrl){
Bitmap bm;
InputStream in;
try{
in = new java.net.URL(sUrl[0]).openStream();
bm = BitmapFactory.decodeStream(new PatchInputStream(in));
File storage = new File(Environment.getExternalStorageDirectory() + File.separator + "/Image/");
Log.i(TAG,"storage:" +storage);
Log.i(TAG,"storage:" +storage.getAbsolutePath());
if(!storage.exists()){
storage.mkdirs();
}
String FileName = "/"+SetConstant.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);
}
catch(IOException e1){
e1.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress){
super.onProgressUpdate(progress);
pDialog.setProgress(progress[0]);
}
protected void onPostExecute(String result){
super.onPostExecute(result);
pDialog.dismiss();
}
}
修改
现在能够根据图像和进度条的工作,以及下载应用程序!但是,我有另外一个问题是如何返回错误信息时,应用程序无法完成下载。目前,当应用程序下载失败就会死机。我认为,我不应该在doInBackground侧内运行。但是,还有什么地方我可以做的检查?任何想法如何返回的错误信息,并要求用户重试崩溃的应用程序呢?
Now the application able to download images according and the progress bar is working as well! But I got another issue is how to return error message when the application failed to complete the download. Currently when the application failed to download it will crash. I believed that I should not run it inside the doInBackground side. But where else can I do the checking? Any idea how to return as an error message and request for the user to retry instead of crashing the application?
推荐答案
您从来没有所谓的doInBackGround(...)中onProgressUpdate;请注意的AsyncTask的跑跑多发性实例是一个<一个href="http://stackoverflow.com/questions/6645203/android-asynctask-avoid-multiple-instances-running">bad想法。以下是我建议:
You never called onProgressUpdate during your doInBackGround(...); Please note that running multipule instance of AsyncTask is a bad idea. Here is what I suggest:
如果(isSyncSuccess){
if (isSyncSuccess) {
SetConstant.IMAGE_EXIST = 1;
pDialog = new ProgressDialog(GalleryScreen.this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setProgress(0);
pDialog.setMax(contentId.length);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
new DownloadFile().execute();
}
}
private class DownloadFiles extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl){
Bitmap bm;
InputStream in;
if (contentId.length>0){
for (int i=0;i<contentId.length;i++){
if(helper.databaseChecking(useremail, contentId[i])){
contentdownload = i;
SetConstant.CONTENT_ID = contentId[i];
String URL = SetConstant.URL_DOWNLOAD_CONTENT+contentId[i];
//YOUR INTRESTING LOOP HERE.
publishProgress(30);
//SOME INTRESTING NUMBER FOR PROGRESS UPDATE
}
}
try{
in = new java.net.URL(sUrl[0]).openStream();
bm = BitmapFactory.decodeStream(new PatchInputStream(in));
File storage = new File(Environment.getExternalStorageDirectory() + File.separator + "/Image/");
Log.i(TAG,"storage:" +storage);
Log.i(TAG,"storage:" +storage.getAbsolutePath());
if(!storage.exists()){
storage.mkdirs();
}
String FileName = "/"+SetConstant.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();
}
catch(IOException e1){
e1.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
pDialog.setProgress(progress[0]);
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
}
}
当然,这code不运行,你需要修复的范围。但我想建议的是你的循环应该是在doInBackGround(...),你应该只的AsyncTask的1个实例,在给定的时间,这种情况下,并调用onProgressUpdate();
Of course this code don't run and you need to fix the scopes. but what I am trying to suggest is your loop should be in doInBackGround(...), you should only have 1 instance of AsyncTask at given time for this case, and call the onProgressUpdate();
这篇关于与AsyncTask的下载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!