简体   繁体   English

如何调用getDialog.dismiss以从另一个类(未嵌套)中的AsyncTask类中消除对话框片段?

[英]How can I call getDialog.dismiss to dismiss Dialog Fragment from AsyncTask Class in another Class(not nested)?

I know that it's possible to call the method getDialog.dismiss if I use AsyncTask as an inner class but due to the amount of code in the class I decided to have another class for AsyncTask now if I try calling the method it says that Non-static method 'getDialog()' cannot be referenced from a static context. 我知道,如果我将AsyncTask用作内部类,则可以调用getDialog.dismiss方法,但是由于该类中的代码量很大,如果我尝试调用方法说它为Non-静态方法'getDialog()'不能从静态上下文中引用。 How can I make it work? 我该如何运作?

public class BackgroundImageResize extends AsyncTask<Uri, Integer, byte[]> 
      {

Bitmap mBitmap;
byte[] mUploadBytes;
Context context;

public BackgroundImageResize(Context ctx) {
    context = ctx.getApplicationContext();
}

public BackgroundImageResize(Bitmap bitmap) {
    if (bitmap != null) {
        this.mBitmap = bitmap;
    }
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    Toast.makeText(context, "compressing image", 
Toast.LENGTH_SHORT).show();
    progressBar.setVisibility(View.VISIBLE);
}

@Override
protected byte[] doInBackground(Uri... params) {

    try {
        mBitmap = 
MediaStore.Images.Media.getBitmap(context.getContentResolver(), 
params[0]);
    } catch (IOException e) {
        e.printStackTrace();
    }

    byte[] bytes;
    bytes = getBytesFromBitmap(mBitmap, 70);
    return bytes;
}

@Override
protected void onPostExecute(byte[] bytes) {
    super.onPostExecute(bytes);
    mUploadBytes = bytes;
    progressBar.setVisibility(View.INVISIBLE);
    //execute the upload task

    ChooseImageActivity.mOnInputListener.sendInput(mUploadBytes);
    ChooseImageActivity.getDialog().dismiss();


}

public static byte[] getBytesFromBitmap(Bitmap bitmap, int quality) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream);
    return stream.toByteArray();
}
}

Pass on a callback on the constructor of the AsyncTask and invoke it onPostExecute . AsyncTask的构造函数上传递回调,并在onPostExecute调用它。

See this 看到这个

I suggest creating an interface: 我建议创建一个接口:

interface MyInterface{
void onTaskFinished(byte[] bytes)
}

Init and use this interface inside your BackgroundImageResize class: 初始化并在BackgroundImageResize类中使用此接口:

myInterface.onTaskFinished(mUploadBytes)

Make a setter for it so you can receive the callback in your Activity. 为此设置一个setter,以便您可以在Activity中接收回调。 Implement this interface inside of your MainActivity and don't forget to use the setter previously created after you init BackgroundImageResize class. 在MainActivity内部实现此接口,并且不要忘记使用初始化BackgroundImageResize类后先前创建的设置器。

Your Activity should now overwrite onTaskFinished(byte[] bytes) and when task is finished you can use: 现在,您的活动应该覆盖onTaskFinished(byte [] bytes),当任务完成时,您可以使用:

getDialog().dismiss();

Hope that helps. 希望能有所帮助。

Pass reference to your activity or the class which has access to your dialog in the constructor of your AsyncTask class. 在AsyncTask类的构造函数中,传递对您的活动或有权访问对话框的类的引用。 And use a weak reference to your activity and access fields like progressBar from that weak reference. 并使用对活动的弱引用,并从该弱引用中访问诸如progressBar之类的字段。 Like this: 像这样:

public class MainActivity extends AppCompatActivity {

    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
    }



    static class BackgroundImageResize extends AsyncTask<Uri, Integer, byte[]> {

        WeakReference<MainActivity> mActivity;

        Bitmap mBitmap;
        byte[] mUploadBytes;

        public BackgroundImageResize(Bitmap bitmap, MainActivity activity) {

            mActivity = new WeakReference<MainActivity>(activity);

            if (bitmap != null) {
                this.mBitmap = bitmap;
            }
       }


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(mActivity.get(), "compressing image",
                    Toast.LENGTH_SHORT).show();
            mActivity.get().progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected byte[] doInBackground(Uri... params) {

            try {
                 mBitmap = MediaStore.Images.Media.getBitmap(mActivity.get().getContentResolver(),
                            params[0]);
            } catch (IOException e) {
                e.printStackTrace();
            }

            byte[] bytes;
            bytes = getBytesFromBitmap(mBitmap, 70);
            return bytes;
        }

        @Override
        protected void onPostExecute(byte[] bytes) {
            super.onPostExecute(bytes);
            mUploadBytes = bytes;
            mActivity.get().progressBar.setVisibility(View.INVISIBLE);
            //execute the upload task


        }

        public byte[] getBytesFromBitmap(Bitmap bitmap, int quality) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream);
            return stream.toByteArray();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM