AsyncTask:doInBackground忙时使用什么?

时间:2018-11-03 15:29:52

标签: android bitmap android-asynctask uri background-process

我必须在我的应用程序中共享图像。

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方法才被调用...我想在记录某些东西时共享图像。

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

1 个答案:

答案 0 :(得分:0)

我能够通过将AsyncTask更改为RxJava来完成这项工作。如果有人碰到这个问题,我将在此处保留代码差异。

var data = { names: [{ name: "mike", taskID: 1 }, { name: "nat", taskID: 2 }], jobs: [{ name: "aaa", taskID: 3 }, { name: "bbb", taskID: 4 }], cities: [ { name: "london", taskID: 5 }, { name: "new york", taskID: 6 }] },
    compareTask = { triggers: [ { name: "Plumbing", taskID: 9 }, { name: "FireDepartment", taskID: 4 } ] },
    result = data.jobs.filter(item =>
        compareTask.triggers.every(({ taskID }) => taskID !== item.taskID));
  
console.log(result);