Reputation: 139
I have this AsyncTask after it is called by another one OnPostExecute()
Method and this is the code
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);
}
}
The function of this code is to check 1st if the image exist in the folder then if not it will upload/download the image from the server depends if it is exist or not.
My problem is i have a progress the must update or increment but nothing happens whats the problem?
Upvotes: 0
Views: 168
Reputation: 3133
when you are using multithreading in your app, you have to use "runOnUiThread" function for updating Views
based on Android Developers Doc :
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.
so in your case you have to do something like this :
runOnUiThread(new Runnable() {
@Override
public void run() {
publishProgress((int) p);
}
});
runOnUiThread is a function from Activity Class so incase that you are not inside a activity you have to pass the Context and use something like
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
publishProgress((int) p);
}
});
Upvotes: 3