gpt4 book ai didi

Android ProgressDialog 显示但进度未更新

转载 作者:太空狗 更新时间:2023-10-29 16:05:19 25 4
gpt4 key购买 nike

我正在尝试在创建后更新进度条(均在 AsyncTask 中)。进度对话框显示并关闭但未更新,它始终设置为 0。这是我的代码: 公共(public)类 ImageTask 扩展 AsyncTask {

    @Override
protected Void doInBackground(String... saleId) {

//Drawable drawable = getActivity().getResources().getDrawable(R.drawable.sale_image_holder);
//Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
//ByteArrayOutputStream stream = new ByteArrayOutputStream();
//bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
//byte[] imageInAsByteArray = stream.toByteArray(); // here
ImgService imgService = new ImgService();
imgService.addImgToSale(imageInAsByteArray, saleId[0], this);
return null;
}

@Override
protected void onProgressUpdate(Integer... progress)
{
super.onProgressUpdate(progress);
dialog.setProgress(progress[0]);
}

@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
}

public void doProgress(int value){
publishProgress(value);
}
}

这是 ImgService 类:

public class ImgService 
{
public void addImgToSale(byte[] img, final String saleId, final ImageTask task)
{
final ParseFile file = new ParseFile(saleId + ".jpeg", img);
try {
file.save();
} catch (ParseException e1) {
e1.printStackTrace();
return;
}
task.doProgress(50);
ParseObject image = new ParseObject(DBDictionary.SALE_IMAGES);
ParseObject sale = new ParseObject(DBDictionary.SALE);
sale.setObjectId(saleId);
image.put(DBDictionary.SALE_IMAGES_SALE_ID_P, ParseObject.createWithoutData(DBDictionary.SALE, saleId));
image.put("image", file);
try {
image.save();
} catch (ParseException e1) {
e1.printStackTrace();
return;
}
task.doProgress(100);
}
}

在网上,我发现了很多关于显示和关闭 ProgressDialog 的问题,而不是关于进度更新的问题。

谢谢!

最佳答案

来自 android 文档:在您的异步任务中使用 onProgressUpdate 函数。

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));//this function updates the progress bar ...
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}

protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}

protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}

关于Android ProgressDialog 显示但进度未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16980202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com