0

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.

Zain
  • 37,492
  • 7
  • 60
  • 84
Raj kumar
  • 153
  • 4
  • 13
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – user1506104 Oct 15 '17 at 07:14
  • That Picasso operation is asynchronous. The image will not have been loaded into the `ImageView` by the time you try to retrieve it in the next line. – Mike M. Oct 15 '17 at 07:16
  • @MikeM. any code snippet please. – Raj kumar Oct 15 '17 at 07:17
  • I don't use Picasso, but it looks like `into()` is pretty flexible. I'd probably do something like is shown in [this answer](https://stackoverflow.com/a/38542241), and get the `Bitmap` you need in the `onBitmapLoaded()` method. – Mike M. Oct 15 '17 at 07:24

2 Answers2

2

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.

frogatto
  • 28,539
  • 11
  • 83
  • 129
0

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);

}
Somnath Kadam
  • 6,051
  • 6
  • 21
  • 37