android - 尝试更新进度条时出现致命异常
问题描述
我正在做一个异步任务来处理由 ASPX 网页生成的 pdf 的下载,以便由谷歌或驱动程序的 pdf 查看器打开。是唯一的方法,如果我将 url 直接传递给 pdf 查看器,它无法处理它并给出错误。因此,我将向您展示我的代码。
public static class OpenPdfFromAspxUrl extends AsyncTask<Void, Integer, Boolean> {
private Activity mActivity;
private String fileUrl;
private String FILENAME = "bmed_document.pdf";
public OpenPdfFromAspxUrl(Activity activity, String fileURL) {
this.mActivity = activity;
this.fileUrl = fileURL;
}
/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialog(mActivity, mActivity.getResources().getString(R.string.common_getting_document), false);
}
/**
* Downloading file in background thread
*/
@Override
protected Boolean doInBackground(Void... voids) {
int count;
try {
URL u = new URL(fileUrl);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = c.getContentLength();
// download the file
InputStream input = new BufferedInputStream(u.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + FILENAME);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress((int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(Integer... progress) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
// setting progress percentage
if (getDialog() != null && getDialog().isShowing()) {
progressBarReference.setProgress(progress[0]);
tvProgressReference.setText(new StringBuilder().append("").append(progress[0]).append(" %").toString());
}
}
});
}
/**
* After completing background task
* Dismiss the progress dialog
**/
@Override
protected void onPostExecute(Boolean stored) {
// dismiss the dialog after the file was downloaded
getDialog().dismiss();
if(stored){
// Displaying downloaded pdf
File newFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + FILENAME);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(newFile), "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
mActivity.startActivity(Intent.createChooser(target, mActivity.getResources().getString(R.string.common_open_with)));
}else{
showAlert(
mActivity,
mActivity.getResources().getString(R.string.common_error_inform),
mActivity.getResources().getString(R.string.common_error_getting_document),
false,
mActivity.getResources().getString(R.string.common_accept),
new OnSingleClickListener() {
@Override
public void onSingleClick(View v) {
getDialog().dismiss();
}
},
null,
null);
}
}
}
在尝试更新进度时,它在下一行中失败了:
progressBarReference.setProgress(progress[0]);
tvProgressReference.setText(new StringBuilder().append("").append(progress[0]).append(" %").toString());
错误是下一个。
android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能接触其视图。
问题是什么?
解决方案
推荐阅读
- javascript - 在输入键VueJS上执行方法?
- javascript - ant.design 表未更新
- vb.net - 在三个面板之间交换位置
- python - 如何动态使用 Python 的类型提示来指示返回值与参数的类型相同
- django - Django Channels 2.x 还是 Ajax?
- certificate - 本地主机 您的连接不是私有的
- java - RestAssured:如何遍历 JSON 对象并验证关键字段的每个值
- php - Magento 错误:函数名称必须是字符串
- python - SQL-您可以在 BigQuery 中删除一行并将删除的行移动到另一个表吗?
- c# - Xamarin Android,如何将 POST 数据发送到 MySQL 数据库中的 PHP 页面?