How to upload an image to Firebase storage?

前端 未结 5 1328
独厮守ぢ
独厮守ぢ 2021-01-04 07:55

I\'m trying to upload a simple byte array into Firebase storage, but my onFailureListener keeps getting called and logging back to me saying that the upload fai

5条回答
  •  温柔的废话
    2021-01-04 08:35

    Simply call this method to store your image to firebase.

     private void storeImageToFirebase() {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8; // shrink it down otherwise we will use stupid amounts of memory
            Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoUri.getPath(), options);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] bytes = baos.toByteArray();
            String base64Image = Base64.encodeToString(bytes, Base64.DEFAULT);
           //For the API less than 28 (Android version 8 )
           //String base64Image = android.util.Base64.encodeToString(bytes, android.util.Base64.DEFAULT);
    
    
            // we finally have our base64 string version of the image, save it.
            firebase.child("pic").setValue(base64Image);
            System.out.println("Stored image with length: " + bytes.length);
        }
    

    For more details see these examples:

    • Sample 1
    • Sample 2

提交回复
热议问题