Compressor icon indicating copy to clipboard operation
Compressor copied to clipboard

Usage for non-UI thread

Open gelbertgel opened this issue 7 years ago • 0 comments

My compress function is inside non-Ui thread. What should I do?

I did something like this. 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();

gelbertgel avatar Jul 30 '18 15:07 gelbertgel