在另一个OnPostExecute()
方法调用之后,我有了这个AsyncTask,这就是代码
class GetDataTask_image extends AsyncTask<Void, Integer, Void> {
public GetDataTask_image() {
super();
}
@Override
protected void onPreExecute() {
GetDataTask_out.Status.values();
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected Void doInBackground(Void... params) {
/* Show checking image */
Cursor data_image = Sync.get_image(current_email);
int image_count = data_image.getCount();
while (data_image.moveToNext()) {
String folder = data_image.getString(data_image.getColumnIndex("folder"));
String file_name = data_image.getString(data_image.getColumnIndex("myimage"));
JSONObject unsync_image1 = new JSONObject();
FilePathname = sdCardDirectory + "/" + file_name;
File file = new File(FilePathname);
Context c = data_syncing_v3.this;
File directory = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
String imgpath = directory + "/" + file_name;
fileName = imgpath;
if (file.exists()) {
try {
unsync_image1.put("module", folder);
unsync_image1.put("name", file_name);
unsync_image1.put("tag", 1);
unsync_images1.put(unsync_image1);
} catch (JSONException e) {
e.printStackTrace();
Log.e("MYAPP", "exception", e);
}
} else {
try {
unsync_image1.put("module", folder);
unsync_image1.put("name", file_name);
unsync_image1.put("tag", 0);
unsync_images1.put(unsync_image1);
} catch (JSONException e) {
e.printStackTrace();
Log.e("MYAPP", "exception", e);
}
}
Log.e("file_name", file_name);
}
final String data = unsync_images1.toString();
stringRequest = new StringRequest(Request.Method.POST, master_link_forimage,
new Response.Listener<String>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onResponse(String response) {
Log.e("tagconvertstr", "[" + response + "]");
try {
obj = new JSONObject(response);
sync_data = obj.getJSONArray("data");
if (sync_data.length() == 0) {
/* Close dialog */
p = (float) index / (float) sync_data.length();
p = p + p * (float) 50;
publishProgress((int) p);
} else {
Context c = data_syncing_v3.this;
final File directory = c.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
final StringBuilder data_image = new StringBuilder("");
for (int i = 0; i < sync_data.length(); i++) {
JSONObject sync_object = sync_data.getJSONObject(i);
String procedure = sync_object.getString("procedure").toString();
String link = sync_object.getString("link").toString();
String file_name = sync_object.getString("file").toString();
String folder = sync_object.getString("folder_name").toString();
if (procedure.toString().equals("up")) {
/* Upload file */
String imgpath = directory + "/" + file_name;
fileName = imgpath;
final File findfile = new File(imgpath);
imgPath = directory + "/" + findfile.getName();
fileName = findfile.getName();
image_view.setImageBitmap(BitmapFactory.decodeFile(imgPath));
image_view.buildDrawingCache();
Bitmap bitmap = ((BitmapDrawable) image_view.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] image = stream.toByteArray();
ConvertImage = Base64.encodeToString(image, Base64.DEFAULT);
data_image.append(folder + "**cut_here**" + fileName.toString() + "**cut_here**" + ConvertImage + "**new_line**");
} else {
/* Download file */
String main_link = "MY LINK FOR DOWNLOAD";
FilePathname = sdCardDirectory + "/" + file_name;
DownloadFilesname(FilePathname, main_link);
}
if (i == (sync_data.length() - 1)) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, uploader,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("MYAPP", "[" + response + "]");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("MYAPP", "3");
back_holder.setVisibility(View.VISIBLE);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("image_data", data_image.toString());
return params;
}
};
VolleySingleton.getInstance(data_syncing_v3.this).addToRequestQueue(stringRequest);
}
index++;
p = (float) index / (float) sync_data.length();
p = p + p * (float) 50;
publishProgress((int) p);
Log.e("MYAPP", "exception 11111");
}
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("MYAPP", "exception", e);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
/* TODO : In case there is an error */
Log.e("MYAPP", "1");
back_holder.setVisibility(View.VISIBLE);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", current_email);
params.put("data", data);
return params;
}
};
VolleySingleton.getInstance(mContext).addToRequestQueue(stringRequest);
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
mProgressAnimation.setProgress(values[0]);
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
此代码的功能是检查1st文件夹中是否存在该图像,如果不存在,它将根据是否存在从服务器上载/下载该图像。
我的问题是我有一个必须更新或递增的进度,但是什么也没发生,这是什么问题?
答案 0 :(得分:3)
在应用中使用多线程时,必须使用“ runOnUiThread”功能来更新视图
基于Android开发人员文档:
Runs the specified action on the UI thread. If the current thread is the UI
thread, then the action is executed immediately. If the current thread is not
the UI thread, the action is posted to the event queue of the UI thread.
因此,在您的情况下,您必须执行以下操作:
runOnUiThread(new Runnable() {
@Override
public void run() {
publishProgress((int) p);
}
});
runOnUiThread是活动类中的一个函数,因此,如果您不在活动内部,则必须传递上下文并使用类似的
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
publishProgress((int) p);
}
});