在我的应用程序中,我使用picasso下载图像并将该图像转换为字节数组。我在下方调用此方法下载并将图像转换为字节数组。Android - 位图空对象引用
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;
}
我的问题是我收到这样的错误。
登录
java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
谁能帮我解决这个问题。
可能重复[什么是NullPointerException,以及如何解决它?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-fix -it) – user1506104
毕加索操作是异步的。当您尝试在下一行中检索图像时,图像将不会被加载到“ImageView”中。 –
@MikeM。请任何代码片段。 –