无法检索Firabase存储映像URL

时间:2019-01-09 12:20:37

标签: android

在我的应用中,用户可以通过我的应用将其图像上传到Firebase存储和数据库中,但是我无法检索上传图像的URL来设置其个人资料图像

private FirebaseAuth mAuth;

private DatabaseReference databaseReference;

private StorageReference UserProfileImageRef;
String currentUserID;

databaseReference=FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
UserProfileImageRef=FirebaseStorage.getInstance().getReference().child("Profile Images");

databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            {
                if(dataSnapshot.exists())
                {
    String q=UserProfileImageRef.getDownloadUrl().toString();
                Toast.makeText(SetupActivity.this, "url"+q, Toast.LENGTH_SHORT).show();
               Glide.with(SetupActivity.this)
                        .load(q)
                        .into(ProfileImage);

            }
        }



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

        }
    });

enter image description here enter code here

2 个答案:

答案 0 :(得分:0)

databaseReference=FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID).child("profileimage");

您忘记添加子级“ profileimage”

更新: 您的下载网址与Firebase存储中的网址不同。我认为您输入了错误的下载网址。

请参阅此链接:https://stackoverflow.com/a/50572357/9346054

filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                Log.d(TAG, "onSuccess: uri= "+ uri.toString());
                //You store the download in database.
                //set value databaseReference=FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID).child("profileimage").setValue(uri.toString()).addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if(task.isSuccessful()){
                    //Success store the link in the database.
                    Toast.makeText( getApplicationContext(), "Success",Toast.LENGTH_SHORT ).show();
                }
            }
        });
            }
        });
    }
});

基于UR的更新: 试试这个

  Uri resulturi = result.getUri();
        final StorageReference filepath = UserProfileImageRef.child(currentUserID + ".jpg");
        filepath.putFile(resulturi).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                if (task.isSuccessful()) {

                    //Okay part ni dia ambik link kat firebase storage akan pergi ke photoUri kat student tuu
                    filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            //Update in database
                            databaseReference.child("profileimage").setValue(String.valueOf(uri));
                            Toast.makeText(SetupActivity.this, "successfully", Toast.LENGTH_SHORT).show();
                            progressDialog.dismiss();
                        }
                    });


                }

            }
        });

答案 1 :(得分:0)

Uri resulturi=result.getUri();
            StorageReference filepath=UserProfileImageRef .child(currentUserID +".jpg");
            filepath.putFile(resulturi).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task)
                {
                    if (task.isSuccessful())
                    {
                        Intent selfIntent=new Intent(SetupActivity.this,SetupActivity.class);
                        startActivity(selfIntent);


                        Toast.makeText(SetupActivity.this, "success", Toast.LENGTH_SHORT).show();
                        final String downloadurl =task.getResult().getStorage().getDownloadUrl().toString();
                        databaseReference.child("profileimage").setValue(downloadurl)
                                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task)
                                    {
                                        if(task.isSuccessful())
                                        {
                                            Toast.makeText(SetupActivity.this, "successfully", Toast.LENGTH_SHORT).show();
                                            progressDialog.dismiss();

                                        }
                                        else
                                        {
                                            String message=task.getException().getMessage();
                                            Toast.makeText(SetupActivity.this, "error"+message, Toast.LENGTH_SHORT).show();
                                            progressDialog.dismiss();
                                        }
                                    }

                                });
                    }

                }
            });
        }