如何在继续之前等待非null返回

问题描述 投票:0回答:1

我想从一个单独的类中检索位图。单独的类具有使用数据回调从在线服务器下载位图的方法。默认情况下,该方法返回null,直到下载位图为止。当我从主类调用该方法时,我将得到null。在继续执行任何主类操作之前,我如何等待此检索为非null(即位图已下载)?

Main Class

profileBitmap = myParse.getImageBitmap(getContext(), tagId);

单独类别

public Bitmap getImageBitmap (final Context context, String tagId) {

        // give us the user's photo
        ParseQuery<ParseObject> parseQuery = new ParseQuery<>("Photo");
        parseQuery.whereEqualTo("username", tagId);
        parseQuery.getFirstInBackground(new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject object, ParseException e) {
                if (object != null && e == null) {
                    ParseFile profilePic = (ParseFile) object.get("profilePicture");
                    //download process
                    profilePic.getDataInBackground(new GetDataCallback() {
                        @Override
                        public void done(byte[] data, ParseException e) {
                            if (data != null && e == null) {
                                // converts file to image
                                selectedImageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                            } else {
                                // returns error user bitmap
                                selectedImageBitmap = BitmapFactory.decodeResource(context.getResources(), android.R.drawable.ic_dialog_alert);
                                FancyToast.makeText(context, "Error: " + e.getMessage(), FancyToast.LENGTH_SHORT, FancyToast.ERROR, false).show();
                            }
                        }
                    });
                } else {
                    // returns empty user bitmap
                    selectedImageBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.empty_user);
                    FancyToast.makeText(context, "Error: " + e.getMessage(), FancyToast.LENGTH_SHORT, FancyToast.ERROR, false).show();
                }
            }
        });
        return selectedImageBitmap;
    } 
java android callback
1个回答
0
投票

您可以为此使用MutableLiveData

public LiveData<Bitmap> getImageBitmapObservable(final Context context, String tagId) {

    MutableLiveData<Bitmap> selectedImageLiveData = new MutableLiveData<Bitmap>;

    // give us the user's photo
    ParseQuery < ParseObject > parseQuery = new ParseQuery < > ("Photo");
    parseQuery.whereEqualTo("username", tagId);
    parseQuery.getFirstInBackground(new GetCallback < ParseObject > () {
        @Override
        public void done(ParseObject object, ParseException e) {
            if (object != null && e == null) {
                ParseFile profilePic = (ParseFile) object.get("profilePicture");
                //download process
                profilePic.getDataInBackground(new GetDataCallback() {
                    @Override
                    public void done(byte[] data, ParseException e) {
                        if (data != null && e == null) {
                            // converts file to image
                            selectedImageLiveData.post(BitmapFactory.decodeByteArray(data, 0, data.length));
                        } else {
                            selectedImageLiveData.post(BitmapFactory.decodeResource(context.getResources(), android.R.drawable.ic_dialog_alert));
                            // returns error user bitmap
                            FancyToast.makeText(context, "Error: " + e.getMessage(), FancyToast.LENGTH_SHORT, FancyToast.ERROR, false).show();
                        }
                    }
                });
            } else {
                // returns empty user bitmap
                selectedImageLiveData.post(BitmapFactory.decodeResource(context.getResources(), R.drawable.empty_user));
                FancyToast.makeText(context, "Error: " + e.getMessage(), FancyToast.LENGTH_SHORT, FancyToast.ERROR, false).show();
            }
        }
    });
    return selectedImageLiveData;
}

用法

getImageBitmapObservable().observe(new Observer<Bitmap>(){
        @Override
        public void onChanged(@Nullable Bitmap response){
            // here you will have your Bitmap obejct
    })
© www.soinside.com 2019 - 2024. All rights reserved.