SaltySea SaltySea - 1 month ago 10
Android Question

Using values outside of overridden method leads to null

In my Post class, within saveBitmap method, I'm using the method from my interface to go to a URL and retrieve the image into a bitmap then return it. It works fine, it does retrieve the bitmap but if I try to use bitmap[0] outside of the interface method (in Figure 1) it says it's null.

So whenever I have to work on the retrieved value, it must be inside the method and it won't let me take it out for some reason. The other problem that follows this (which I think could be tied) is that I need to override the method onDownloadComplete twice. At the very bottom of my Post class, you can see it's overridden again with an empty body. I have that just so the very top line of my Post class, where I implement onDownloadImageListener, doesn't red-line.

I'm not sure how to fix this, I would like to be able to retrieve the values outside of the the listener method.

Post class:

public class Post extends Fragment implements OnDownloadImageListener {
...
private void saveBitmap(String userName, String key){
DownloadImage downloadImage = new DownloadImage();
final Bitmap[] bitmap = new Bitmap[1];
final String url = "https://graph.facebook.com/" + key + "/picture?type=large";

downloadImage.execute(url);
downloadImage.setOnDownloadImageListener(new OnDownloadImageListener() {
@Override
public void onDownloadComplete(Bitmap bmap) {
bitmap[0] = bmap;
}
});
//**FIGURE 1**
}

@Override
public void onDownloadComplete(Bitmap bmap) { //I need this otherwise it gives me an error on the top line where I have "implements OnDownloadImageListener"


DownloadImage class:

public class DownloadImage extends AsyncTask<String, Void, Bitmap> {

private OnDownloadImageListener listener;

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected Bitmap doInBackground(String... URL) {

String imageURL = URL[0];

Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}

@Override
protected void onPostExecute(Bitmap result) {
if(listener!=null){
listener.onDownloadComplete(result);
}
}

public void setOnDownloadImageListener(OnDownloadImageListener listener){
this.listener=listener;
}
}


and finally, My interface/listener:

public interface OnDownloadImageListener {
void onDownloadComplete(Bitmap bmap);
}

Answer

You should pass in this (referring to the class Post that implements the interface OnDownloadImageListener) into the setOnDownloadImageListener method of the DownloadImage object downloadImage.

public class Post extends Fragment implements OnDownloadImageListener {
...
private void saveBitmap(String userName, String key){
    DownloadImage downloadImage = new DownloadImage();
    final Bitmap[] bitmap = new Bitmap[1];
    final String url = "https://graph.facebook.com/" + key + "/picture?  type=large";

    downloadImage.execute(url);
    downloadImage.setOnDownloadImageListener(this);
    //**FIGURE 1**
}

@Override
public void onDownloadComplete(Bitmap bmap) { 
    bitmap[0] = map;
}