Issue
I used compressed image library : https://github.com/zetbaitsu/Compressor. This library must run UI thread but compress function is inside non-Ui thread. So I moved this function inside asynctask.
Is this the right way and do you have any other idea? Use asynctask execute()
correctly or should I use executeOnExecutor()
or not use get()
function?
public static class compressFile extends AsyncTask<Void, File, File> {
@Override
protected File doInBackground(Void... params) {
File thumbFile = null;
try {
thumbFile = new Compressor(MyApplication.getContext())
.setMaxWidth(width)
.setMaxHeight(height)
.setQuality(quality)
.setCompressFormat(Bitmap.CompressFormat.JPEG)
.compressToFile(new File(path), " " + System.currentTimeMillis());
} catch (Exception e) {
Mylog.printStackTrace(TAG + " error", e);
}
return thumbFile;
}
}
compress image usage in non-UI thread:
File compressedBigFile = new compressFile().execute().get();
Solution
No, that's not right. NEVER use AsyncTask.get on the UI thread. Doing so forces the UI thread to wait for the result. This is equivalent (actually slightly worse) than not using a thread at all. The correct thing to do is to put any code that requires the result in onPostExecute of the AsyncTask so it is run when the task finishes, and if necessary put up a loading UI while it processes.
Answered By - Gabe Sechan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.