I want to retrieve a bitmap from a separate class. The separate class has a method that downloads a bitmap from an online server with a data callback. By default the method returns null until the bitmap is downloaded. When I call the method from my main class I get null. How can I wait for this retrieval to be non null (ie the bitmap has been downloaded) before continuing any main class operations?
Main Class
profileBitmap = myParse.getImageBitmap(getContext(), tagId);
Separate Class
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;
}
I would use a listener
:
Create an interface
in a new file:
public interface onBitmapDownloadedListener{
void onDownloadFinished(Bitmap bitmap);
}
In your main class implement the interface
, and pass the reference of your main class to the MyParse
class
class MainClass implements onBitmapDownloadedListener{
//now when you create an instance of myParse, pass the listener
......
myParse = new MyParse(this);
@Override
public void onDownloadFinished(Bitmap bitmap){
//here you get the bitmap
profileBitmap = bitmap
}
Make a constructor in MyParse
class that accepts the interface:
class MyParse {
private onBitmapDownloadedListener listener;
//constrcutor
public MyParse(onBitmapDownloadedListener listener){
this.listener = listener
}
//your method
public Bitmap getImageBitmap (final Context context, String tagId) {
........
........
........
@Override
public void done(byte[] data, ParseException e) {
if (data != null && e == null) {
// converts file to image
selectedImageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//notify here
listener.onDownloadFinished(selectedImageBitmap);
}
....
....
....
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.