简体   繁体   中英

Android - Bitmap Null object reference

In my application I am downloading the image using Picasso and converting that image in to byte array.i am calling below this method to download and convert the image to byte array.

   private byte[] convertToByte(String url) {

    Picasso.with(list_my_posts.this).load(url).fit().centerCrop().into(img);
    Bitmap bitmap=((BitmapDrawable)img.getDrawable()).getBitmap();
    ByteArrayOutputStream stream=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
    byteArray= stream.toByteArray();

    Toast.makeText(getApplicationContext(),"Downloaded Successfully",Toast.LENGTH_LONG).show();

    return byteArray;
}

My problem is I am getting error like this.
Log

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference

Can anyone help me to solve this issue.

You don't need a ImageView merely for downloading a image and getting its byte array. Using Picasso you can register a callback to be called when download completes.

private Target target = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
    }
}

Using this callback, you can asynchronously download images:

Picasso.with(context).load(url).into(target);

Also to convert a Bitmap to a byte array, you can first compress the bitmap and then save it into a output stream:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

If you don't want to compress, you can use Bitmap.copyPixelsToBuffer method.

Thanks @frogatto, here I give detail example,

.....
@Override
protected void onCreate(Bundle savedInstanceState) {
    .....

    callingMethod();

}

//any method where you need byte array from image url
private void callingMethod() {

    Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            // Bitmap is loaded, use image here
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] image_arr = stream.toByteArray();
        }

        @Override
        public void onBitmapFailed(Exception e, Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };

    Picasso.get().load(imageURL).into(target);

}

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.

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