在我的應用程序中,我使用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。請任何代碼片段。 –