how to get image downloadable url from firebase storage and store in firebase database?

喜夏-厌秋 提交于 2020-01-24 19:56:27

问题


I am storing users profile pictures in firebase storage. I want to get the downloadable URL of those profile pictures and store each users profile picture URL under his/her User ID in firebase database. Here is my code for storing the picture in firebase storage. But i don't know how to get the URL and store in database under respective user's ID. I have also attached the snapshot of my firebase database. Firebase database image

protected void onActivityResult(int requestCode, int resultCode, Intent 
data) {
    super.onActivityResult(requestCode, resultCode, data);


    if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK)
    {
        progressDialog.setMessage("Uploading...");
        progressDialog.show();
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        String userID = user.getUid();
        Uri uri = data.getData();
        ivProfilePicture.setImageURI(uri);

        StorageReference filepath = 
        storageReference.child("Photos").child(userID);

        filepath.putFile(uri).addOnSuccessListener(new 
        OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Toast.makeText(EditProfile.this,"Upload 
            Done",Toast.LENGTH_SHORT).show();

                progressDialog.dismiss();

            }
        });

回答1:


1> Create a map for some information...

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    String email = user.getEmail();
    String name = user.getDisplayName();
    String userName = "USER_NAME";   // set userName here...
    String id = user.getUid();

    Map<String,String> imageInfo = new HashMap<>();
    imageInfo.put("email", email);
    imageInfo.put("id", id);
    imageInfo.put("name", name);
    imageInfo.put("userName", userName);

2> After uploading file , add information to database..

    // inside OnSuccessListener

    DatabaseReference db = FirebaseDatabase.getInstance().getReference("Users");
    db.push().setValue(imageInfo);

    // this will add all information to your database...

3> Downloading image with file name...

Get file name from database using database query... And use these codes to download image and set to any imageView...

    try {
        final File tmpFile = File.createTempFile("img", "png");
        StorageReference reference = FirebaseStorage.getInstance().getReference("Photos");

        //  "id" is name of the image file....

        reference.child(id).getFile(tmpFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {

                Bitmap image = BitmapFactory.decodeFile(tmpFile.getAbsolutePath());

                userImageView.setImageBitmap(image);

            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }


来源:https://stackoverflow.com/questions/44461515/how-to-get-image-downloadable-url-from-firebase-storage-and-store-in-firebase-da

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