简体   繁体   中英

Get images from firebase storage and realtime database

So, I wanna make the user upload multiple images at the same time to firebase storage. I have done that part successfully but now I want to retrieve the images back. At first, I was uploading one image per user and save the image id to the realtime database so it was very easy to retrieve the image back by the image id. But now how can I save more than one image to the realtime database per user I can't use the same child name because it will be replaced with the old one. Any ideas??

Uploding images and setting the id on realtime database:

private void Fileuploader() throws FileNotFoundException {
        String imageid;

        progress.showProgress(profuctadd.this,"Loading...",false);
        DatabaseHelper databaseHelper = new DatabaseHelper(profuctadd.this);
        Cursor getimage = databaseHelper.GetPath();
        int count = 0;
        while (getimage.moveToNext()){
            Bitmap bm = BitmapFactory.decodeFile(getimage.getString(0));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 35, out);
            imageid = arabic.getText().toString()+"-"+System.currentTimeMillis()+"_"+(count++)+"."+getExtension(uri);
            System.out.println("IMAGES UPLOADEDDDD: "+ imageid);
            String id = getIntent().getStringExtra("storeid");
            member.setImageid(imageid);
            reference.child(id).child(arname).setValue(member);
            byte[] data = out.toByteArray();
            StorageReference Ref = mStorageRef.child(imageid);
            Ref.putBytes(data)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            // Get a URL to the uploaded content
                            //Uri downloadUrl = taskSnapshot.getDownloadUrl();
                            //Toast.makeText(profuctadd.this,"Image uploaded",Toast.LENGTH_LONG).show();
                            progress.hideProgress();
                            Intent intent = new Intent(profuctadd.this,showProducts.class);
                            intent.putExtra("spec",getIntent().getStringExtra("spec"));
                            intent.putExtra("storeid",getIntent().getStringExtra("storeid"));
                            startActivity(intent);
                            DatabaseHelper mDatabaseHelper = new DatabaseHelper(profuctadd.this);
                            Cursor cursor2 = mDatabaseHelper.DeleteDataOfTableImagesAr();
                            cursor2.moveToNext();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                            // Handle unsuccessful uploads
                            // ...
                            Toast.makeText(profuctadd.this,"Failed",Toast.LENGTH_LONG).show();
                        }
                    });

        }





    }

My firebase db:

Firebase 数据库

you can save more than one image of specific user in images node like that.

DatabaseReference dr =  FirebaseDatabase.getInstance().getReference("tL131);

 dr.child("images").push().setValue(String.ValueOf(taskSnapshot.getDownloadUrl()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM