how to add multiple images into firebase database

為{幸葍}努か 提交于 2020-03-06 09:24:06

问题


How can I add multiple images into Firebase Realtime Database

My Code

private void uploadProductToFirebase() {

        final StorageReference imageFilePath = ProductImagesRef;

        for (int uploadCount = 0; uploadCount<ImageList.size(); uploadCount++){
            Uri IndividualImage= ImageList.get(uploadCount);
            final StorageReference ImageName = imageFilePath.child(pname);
            final int finalUploadCount = uploadCount;
            ImageName.putFile(IndividualImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    ImageName.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            String url = String.valueOf(uri);
                            DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("ProductImages").child(productkey);
                            Product product = new Product();
                            if (finalUploadCount == 0){
                                product.setImage1(url);
                            }
                            else if (finalUploadCount == 1){
                                product.setImage2(url);
                            }
                            else if (finalUploadCount == 2){
                                product.setImage3(url);
                            }
                            databaseReference.setValue(product);
                        }
                    });
                }
            });
        }
    }    

If i add these three images only the 3rd image is stored into database.

What should I change to be able to upload 3 images to the Firebase Storage and Firebase RealTime Database?


回答1:


Suggestion

I would test the following code to see if the problem is with your Product class.

String url = String.valueOf(uri);
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("ProductImages").child("1234");
Product product = new Product();
if (finalUploadCount == 0){
 databaseReference.child("image_1").setValue(url);
}
else if (finalUploadCount == 1){
   databaseReference.child("image_2").setValue(url);
}
else if (finalUploadCount == 2){
   databaseReference.child("image_3").setValue(url);
}

Your might be only saving the last Firebase Storage. Check the Firebase Storage is creating the image, if it is this might be a problem on the line:

 databaseReference.setValue(product);

Also, there is a working example that I implemented in Kotlin.

It gets some image from the user gallery and upload to Firebase Storage, after that it stores the Firebase Storage Reference to my Realtime Database.

So my advice is for you to try to work with Bitmap, then do a task for upload it to Firebase Storage. You might consider to compress your image before uploading to not consume all your storage space (It might get expensive if you don't do that)

Kotlin Example

private fun handleImageUpload() {
        val filePath : StorageReference = FirebaseStorage.getInstance().reference.child("user_image").child(mAuth.currentUser!!.uid)

        var  bitmap: Bitmap? = null

        try {
            bitmap = MediaStore.Images.Media.getBitmap(application.contentResolver, resultUri)
        } catch (e : IOException) {
            e.printStackTrace()
        }

        val boas = ByteArrayOutputStream()
        bitmap!!.compress(Bitmap.CompressFormat.JPEG, 20, boas)
        val data = boas.toByteArray()
        val uploadTask = filePath.putBytes(data)

        uploadTask.continueWithTask { task ->
            if (!task.isSuccessful) {
                task.exception?.let {
                    throw it
                }
            }
            filePath.downloadUrl
        }.addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val downloadUri = task.result
                val newImage = HashMap<String, Any>()
                newImage.put("profileImageUrl", downloadUri.toString())
                mCustomerDatabase.updateChildren(newImage)
            } else {
                // Handle failures
                // ...
            }
        }
    }

Java Example

https://github.com/jirawatee/FirebaseStorage-Android/blob/master/app/src/main/java/com/example/storage/UploadActivity.java



来源:https://stackoverflow.com/questions/60100311/how-to-add-multiple-images-into-firebase-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!