简体   繁体   中英

how to get an image path in firebaseStorage to firebaseDatabase

hi every one am making an app that use firebaseDatabase for Users info and firebaseStorage to save the image and i want to put the path of the image uploaded to firebaseStorage in firebaseDatabase under User info so i can display the image like profile pic so am using this to upload image and mainActivity to show the image. i do search for solution and got some but it didnt work or i didnt know how to make it work. am new at android

upload image

 public String getExtension(Uri uri) {
    ContentResolver contentResolver = getContentResolver();
    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    return mimeTypeMap.getMimeTypeFromExtension(contentResolver.getType(uri));
}

private void uploadImage() {
 StorageReference reference = mStorage.child(System.currentTimeMillis()+"."+getExtension(imageUri));


    uploadTask= reference.putFile(imageUri)
            .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(AddImageActivity.this,"Image Uploaded",Toast.LENGTH_SHORT).show();

                    Intent ii = new Intent(AddImageActivity.this,MainActivity.class);
                    startActivity(ii);
                    finish();

                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // Handle unsuccessful uploads
                    // ...
                }
            });

}

MainActvity

 public class MainActivity extends AppCompatActivity {
 ImageView profilePic ;
 TextView nameProfile ;
  DatabaseReference databaseReference ;
 FirebaseUser fUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    profilePic=findViewById(R.id.imageProfile);
    nameProfile = findViewById(R.id.nameProfile);

   fUser=FirebaseAuth.getInstance().getCurrentUser();
   databaseReference = FirebaseDatabase.getInstance().getReference("Users").child(fUser.getUid());

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            User user = snapshot.getValue(User.class);
            nameProfile.setText(user.getName());
            Glide.with(getApplicationContext()).load(user.getImageURL()).into(profilePic);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });


}

}

that work for me

final StorageReference reference = mStorageReference.child(System.currentTimeMillis() + "." + getExtension(imageUri));
    reference.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            taskSnapshot.getStorage().getDownloadUrl()
                    .addOnSuccessListener(
                            new OnSuccessListener<Uri>() {
                                @Override
                                public void onSuccess(Uri uri) {
                                    String imageUrl = uri.toString();
                                 
                                }
                            }
                    );
        }
    });
 public void uploadfile(){
   if(uri!=null){
       final StorageReference filereference=storageReference.child(System.currentTimeMillis()+"."+getFileExtension(uri));

       filereference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
           @Override
           public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {

            filereference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    Upload upload=new Upload(editText1.getText().toString(),uri.toString());
                    String str=databaseReference.push().getKey();
                    databaseReference.child(str).setValue(upload);

                    progressBar.setVisibility(View.INVISIBLE);
                    progressBar2.setVisibility(View.VISIBLE);
                    final Timer timer=new Timer();
                    TimerTask timerTask=new TimerTask() {
                        @Override
                        public void run() {
                            counter++;
                            progressBar2.setProgress(counter);
                            if (counter==100){
                                toast.show();
                                progressBar2.setVisibility(View.INVISIBLE);
                                counter=0;
                                timer.cancel();
                            }
                        }
                    };timer.schedule(timerTask,1,15);
                }
            });
           }
       }).addOnFailureListener(new OnFailureListener() {
           @Override
           public void onFailure(@NonNull Exception e) {

           }
       });
   }
   else {
       Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show();
   }
}

Hello my friend please copy-paste your function like this

private void uploadImage() {
   StorageReference reference = mStorage.child(System.currentTimeMillis()+"."+getExtension(imageUri));

uploadTask= reference.putFile(imageUri)
        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

               reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                              @Override
                                                public void onSuccess(Uri uri) {
                                                        
                                                        String uploadedImageUrl = uri.toString();
                                                        
            // Now you have your image in (uploadedImageUrl) variable so write your code to upload in firebaseDatabase and enjoy
                                                    }
                                                });

                Toast.makeText(AddImageActivity.this,"Image Uploaded",Toast.LENGTH_SHORT).show();

                Intent ii = new Intent(AddImageActivity.this,MainActivity.class);
                startActivity(ii);
                finish();

            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
                // ...
            }
        });

}

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