0
我試圖在android中使用imageView時上傳圖像,並且它成功上傳了圖像。但是當我在imageView的地方使用imageButton時,我得到了錯誤。我可以使用imgaeButton將圖像上傳到firebase存儲
imageButton.setDrawingCacheEnabled(true);
imageButton.buildDrawingCache();
Bitmap bitmap = imageButton.getDrawingCache();
ByteArrayOutputStream boas = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, boas);
imageButton.setDrawingCacheEnabled(false);
byte[] data = boas.toByteArray();
String path = "firememes/" + UUID.randomUUID() + ".png";
StorageReference firememRef = storage.getReference(path);
我收到此錯誤
07-19 17:17:17.154 21469-21469/com.umer.listview D/AndroidRuntime: Shutting down VM
07-19 17:17:17.154 21469-21469/com.umer.listview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.umer.listview, PID: 21469
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.umer.listview.MainActivity$2.onClick(MainActivity.java:125)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
可能重複[什麼是NullPointerException,以及如何解決它?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-doi-i-修復它) –
我得到錯誤在這行代碼bitmap.compress(Bitmap.CompressFormat.PNG,100,boas); –
是的,我們知道。錯誤消息說明了這一點 –