Home > Net >  Android: why the firebase Push key doesn't change
Android: why the firebase Push key doesn't change

Time:11-30

So i am uploading images to firebase storage and the link would be saved inside realTime firebase

now iam using for loop to upload the images, i want to make the image name uploaded to storage to be the push id firebase give to realTime.

but for some reason image would be saved with different push id inside storage successfully , but when saved to realtime id would be same and loop would keep updating inside same ID. imagePath is global Variable.

for (uploadCount  = 0; uploadCount < ImageList.size(); uploadCount  ) {
        
    imagePath = productRef.child(UID).child("images").push().getKey();
    Uri IndividualImage = ImageList.get(uploadCount);
    StorageReference ImageName = productImagesRef.child(UID).child(imagePath);

    //Compress Images
    Bitmap bmp = null;
    try {
        bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), IndividualImage);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos);
    byte[] data = baos.toByteArray();
    //End of compressing

    //start on uploading compressed
    ImageName.putBytes(data).addOnSuccessListener(taskSnapshot -> ImageName.getDownloadUrl()
            .addOnSuccessListener(uri -> {
        String url = String.valueOf(uri);
        StoreLink(url);
    }));
}

this the firebase realtime upload:

private void StoreLink(String url) {
    HashMap<String ,Object> hashMap = new HashMap<>();
    hashMap.put("image", url);
    hashMap.put("id", imagePath);
    dialog.dismiss();

    assert imagePath != null;
    productRef.child(UID).child("images").child(imagePath).updateChildren(hashMap).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            finish();
        }
    });
}

CodePudding user response:

You're not passing imagePath to your StoreLink function anywhere, which makes me suspect it's coming from the context somewhere and you're just seeing the last value of it.

This fix it, change your StoreLink function to:

                                //                      
  • Related