AsyncTask:doInBackground 繁忙时使用什么?

AsyncTask: What to use when doInBackground busy?

我必须在我的应用中分享一张图片。

Glide.with(PhotoFullScreenActivity.this)
         .asBitmap()
         .load(getBestDownscaledAlternative(mPhotoList.get(mCurrentPosition)))
         .into(new SimpleTarget<Bitmap>() {
             @Override
             public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                 startShareImageTask(resource);
             }

             @Override
             public void onLoadFailed(@Nullable Drawable errorDrawable) {
                 super.onLoadFailed(errorDrawable);
                 Log.e("Check","error");
             }
         });

我使用上面的代码片段将 URL 转换为位图,资源准备就绪后,我调用 startShareImageTask 创建文件。

startShareImageTask 如下所示:

private void startShareImageTask(Bitmap resource){

    new AsyncTask<Bitmap, Void, Uri>(){

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mShareLoadingPB.setVisibility(View.VISIBLE);
            Log.d("ShareImageTask", "onPreExecute: " + System.currentTimeMillis());
        }

        @Override
        protected Uri doInBackground(Bitmap... bitmaps) {
            Uri bmpUri = null;
            try {
                // Use methods on Context to access package-specific directories on external storage.
                // This way, you don't need to request external read/write permission.
                File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
                FileOutputStream out = new FileOutputStream(file);
                resource.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.close();

                // wrap File object into a content provider. NOTE: authority here should match authority in manifest declaration
                bmpUri = ImageFileProvider.getUriForFile(PhotoFullScreenActivity.this, getApplicationContext().getPackageName() + ".fileprovider", file);  // use this version for API >= 24

            } catch (IOException e) {
                e.printStackTrace();
                mShareLoadingPB.setVisibility(View.GONE);
            }
            return bmpUri;
        }

        @Override
        protected void onPostExecute(Uri uri) {
            super.onPostExecute(uri);
            mShareLoadingPB.setVisibility(View.GONE);

            mShareIntent = new Intent();
            mShareIntent.setAction(Intent.ACTION_SEND);
            mShareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            mShareIntent.setType("image/*");

            startActivity(Intent.createChooser(mShareIntent, "Share image"));
        }
    }.execute();
}

一切顺利,但我的应用程序需要在后台任务中录制一些内容,当它录制时,我的 doInBackground 方法在我完成录制之前不会被调用...我想在录制时共享图像东西。

当我的后台线程忙于其他任务时,我该怎么办? 有什么解决方法吗?

我能够通过将 AsyncTask 更改为 RxJava 来完成这项工作。我会在这里留下代码差异,以防有人遇到这个问题。

 mShareLoadingPB.setVisibility(View.VISIBLE);
    Log.d("ShareImageTask", "onPreExecute: " + System.currentTimeMillis());

    Observable.fromCallable(() -> {
        Uri bmpUri = null;
        try {
            // Use methods on Context to access package-specific directories on external storage.
            // This way, you don't need to request external read/write permission.
            File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
            FileOutputStream out = new FileOutputStream(file);
            resource.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.close();

            // wrap File object into a content provider. NOTE: authority here should match authority in manifest declaration
            bmpUri = ImageFileProvider.getUriForFile(PhotoFullScreenActivity.this, getApplicationContext().getPackageName() + ".fileprovider", file);  // use this version for API >= 24

        } catch (IOException e) {
            e.printStackTrace();
            mShareLoadingPB.setVisibility(View.GONE);
        }
        return bmpUri;
    })
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new DefaultObserver<Uri>() {
                @Override
                public void onNext(Uri uri) {
                    mShareLoadingPB.setVisibility(View.GONE);

                    mShareIntent = new Intent();
                    mShareIntent.setAction(Intent.ACTION_SEND);
                    mShareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                    mShareIntent.setType("image/*");

                    startActivity(Intent.createChooser(mShareIntent, "Share image"));
                }

                @Override
                public void onError(Throwable e) {
                    Log.e("Error","Error");
                }

                @Override
                public void onComplete() {

                }
            });