[英]Show Progress Dialog while Processing OnActivityResult
我写了下面的代码,当用户点击附加按钮选择照片时。
下面是相同的代码。
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Select file to upload "), 1);
下面是 OnActivityResult 的代码
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (null != data) { // checking empty selection
if (null != data.getClipData()) { // checking multiple selection or not
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
Uri uri = data.getClipData().getItemAt(i).getUri();
Log.i(TAG, "Path" + getPath(uri));
filespath.add(getPath(uri));
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(getPath(uri), options);
bitmaps.add(bitmap);
}
} else {
Uri uri = data.getData();
}
}
}
}
现在用户可以选择多张照片,我意识到当照片超过 10 张时,我会收到警告在主线程上完成了太多工作。 当用户在选择照片后单击“完成”按钮时,我有 Recycler 视图,其中显示用户在最终上传之前选择的图像的缩略图。
现在的问题是当用户单击完成并直到它显示缩略图时,我如何显示 ProgressDialog ,处理冻结屏幕并避免在主线程上完成警告工作。
为了使解析和加载工作脱离主线程,您可以将所有内容包装在 AsyncTask 中。 鉴于显示的代码量很少,我不知道上面的函数是做什么的,因此可能需要稍微调整一下。 移动所有解析逻辑等在后台执行:
AsyncTask<Void, Void, List<Bitmap>>() {
@Override
protected void onPreExecute()
{
//show progress dialog
//Are you trying to prevent the user from clicking on the UI while updating?
//You can do something such as:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
@Override
protected List<Bitmap> doInBackground(Void... voids) {
//perform the logic, this will return a list of all the Bitmaps to onPostExecute
//Do NOT perform any ui logic here.
}
@Override
protected void onPostExecute(List<Bitmap> bitmaps) {
//cancel progress dialog.
//Update the UI with the response.
//Clear the window lock:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.