代码之家  ›  专栏  ›  技术社区  ›  Vora Ravi

无法检索FiraBase存储图像URL

  •  0
  • Vora Ravi  · 技术社区  · 7 年前

    在我的应用程序中,用户可以通过我的应用程序在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 回复  |  直到 7 年前
        1
  •  0
  •   Ticherhaz FreePalestine אור מזרחי    7 年前
    databaseReference=FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID).child("profileimage");
    

    你忘了添加孩子“profileimage”

    更新: 您的下载URL与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();
                    }
                }
            });
                }
            });
        }
    });
    

    根据您的答案更新: 试试这个

      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();
                            }
                        });
    
    
                    }
    
                }
            });
    
        2
  •  0
  •   Vora Ravi    7 年前
    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();
                                            }
                                        }
    
                                    });
                        }
    
                    }
                });
            }