使用 asyncTask 下载映像



我正在尝试加强我对 asyncTask 的了解,所以我按照教程下载了图像,但我不明白事情,我收到错误 ArrayOutofBound 索引,所以如果有人告诉我原因,我将不胜感激 这是代码,请告诉我他为什么要迭代进度条最大值

public class MainActivity extends AppCompatActivity {
ProgressBar progessBar;
ImageView MyImage;
private final String IMAGE_URL =    "http://www.101apps.co.za/images/headers/101_logo_very_small.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyImage = findViewById(R.id.imageView);
ImageButton downloaad = findViewById(R.id.imageButton);
downloaad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new downloadFile(MainActivity.this).execute(IMAGE_URL);
}
});
}
public class downloadFile extends AsyncTask<String , Integer , String>{
private  int PROGRESS_BAR_MAX = 100;
Context context;
public downloadFile(Context cxt){
this.context = cxt;
}
@Override
protected String doInBackground(String... strings) {
String result = "";
String path = context.getFilesDir() + File.pathSeparator + "downloadFile.png";
HttpURLConnection httpURLConnection = null;
try{
final URL url  = new URL(strings[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
int length = httpURLConnection.getContentLength();
InputStream IS = (InputStream) url.getContent();
byte[] imageData = new byte[length];
Log.d("aho" , "" + length);
int buffersize = (int) Math.ceil(length/(double)PROGRESS_BAR_MAX);
int download = 0;
for(int i = 1 ; i < PROGRESS_BAR_MAX ; i++){
long read = IS.read(imageData , download , buffersize);
download += read;
publishProgress(i);
}
BufferedInputStream bufferedReader = new BufferedInputStream(httpURLConnection.getInputStream());
FileOutputStream Fo = new FileOutputStream(path);
int data = bufferedReader.read();
while(data != -1){
Fo.write(data);
data = bufferedReader.read();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(httpURLConnection == null){
httpURLConnection.disconnect();
}
}
publishProgress(PROGRESS_BAR_MAX);
return path;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progessBar = findViewById(R.id.progressBar);
progessBar.setMax(PROGRESS_BAR_MAX);

}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progessBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s != null) {
progessBar.setVisibility(View.INVISIBLE);
Bitmap bitmap = BitmapFactory.decodeFile(s);
MyImage.setImageBitmap(bitmap);
}
}
}
}

这是堆栈跟踪 01-20 03:37:40.607 4075-4096/com.gamecodeschool.bodybuilder E/Android运行时:致命异常:异步任务#1 流程: com.gamecodeschool.bodybuilder, PID: 4075 java.lang.RuntimeException:执行 doInBackground() 时出错 at android.os.AsyncTask$3.done(AsyncTask.java:353) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383) at java.util.concurrent.FutureTask.setException(FutureTask.java:252) at java.util.concurrent.FutureTask.run(FutureTask.java:271) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) 由以下原因引起:java.lang.ArrayIndexOutOfBoundsException: size=1123 offset=1116 byteCount=12 at com.android.okhttp.okio.Util.checkOffsetAndCount(Util.java:29) at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:368) at com.gamecodeschool.bodybuilder.MainActivity$downloadFile.doInBackground(MainActivity.java:66) at com.gamecodeschool.bodybuilder.MainActivity$downloadFile.doInBackground(MainActivity.java:45) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266)

这是因为您的读取循环循环遍历进度计数,而不是它正在下载的资源的大小。循环遍历资源的大小(请务必检查读取错误),并根据读取量与总长度计算进度。

最新更新