I have been trying to figure out why bitmap is giving me a size value within the SimpleTarget but is null soon as am outside the SimpleTarget.
private Bitmap getImage(String path){
final Bitmap[] bitmaps = new Bitmap[1];
Glide.with(getApplicationContext())
.load(path)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
bitmaps[0] = resource;
Log.v(TAG, "bitmap: " + bitmaps[0].getByteCount());// Returns a value as expected.
}
});
Log.v(TAG, "bitmap2: " + bitmaps[0].getByteCount());// throws a null object reference.
return bitmaps[0];
}
Edit: AsyncTask approach.
private Bitmap getImage(String path){
final Bitmap[] bitmaps = new Bitmap[1];
new AsyncTask<Void, Void, Void>() {
Bitmap tmp;
@Override
protected Void doInBackground(Void... params) {
try {
tmp = Glide.with(getApplicationContext())
.load("http://i.annihil.us/u/prod/marvel/i/mg/b/40/image_not_available.jpg")
.asBitmap()
.into(-1,-1)
.get();
} catch (final ExecutionException | InterruptedException e) {
Log.e(TAG, e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void dummy) {
if (null != tmp) {
// The full bitmap should be available here
bitmaps[0] = tmp;
Log.v(TAG, "bitmap: " + bitmaps[0].getHeight());
Log.d(TAG, "Image loaded");
};
}
}.execute();
Log.v(TAG, "bitmap2: " + bitmaps[0].getHeight());// throws a null object reference.
return bitmaps[0];
}
Edit: Added log in relation to the issue.
08-04 19:35:03.687 5183-5183/com.example.comics V/com.example.comics.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getHeight()' on a null object reference
08-04 19:35:03.709 5183-5183/com.example.comics V/com.example.comics.backend.services.BackgroundService: bitmap: 537
Edit: If bitmap is not defined as final it will shout being accessed from inner class. Declare as final.