如何从覆盖方法Java调用值?

时间:2018-07-19 09:37:35

标签: java android firebase-realtime-database

我如何从替代方法中获取价值?

这是我的代码,我厌倦了将URL设置为setValue(url),但我没得到

String url =dlUri.toString(); this value

child("downloadurl").setValue(url);

enter image description here

public void upload(View view){

    UUID uuidImage = UUID.randomUUID();

    String imageName = "images/"+uuidImage+".jpg";

    final StorageReference storageReference = mStorageRef.child(imageName);

    storageReference.putFile(selected).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {



        @SuppressWarnings("VisibleForTests")
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


            UUID uuid = UUID.randomUUID();
            final String uuidString = uuid.toString();

            storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    Uri dlUri = uri;
                    String url =dlUri.toString();
                }
            });

            FirebaseUser user = mAuth.getCurrentUser();
            String userEmail = user.getEmail().toString();
            String userComment = commentText.getText().toString();


            myRef.child("Posts").child(uuidString).child("useremail").setValue(userEmail);
            myRef.child("Posts").child(uuidString).child("comment").setValue(userComment);
            myRef.child("Posts").child(uuidString).child("downloadurl").setValue(url);




            Toast.makeText(getApplicationContext(),"Post Shared",Toast.LENGTH_LONG).show();

            Intent intent = new Intent(getApplicationContext(), FeedActivity.class);
            startActivity(intent);

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(getApplicationContext(), e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
        }
    });

}

3 个答案:

答案 0 :(得分:1)

这是因为String url = dlUri.toString();不在您要访问它的范围内。因为我假设您仍然需要等待该值,所以为什么不将所有代码移到onSuccess()内呢?

答案 1 :(得分:0)

url之外声明storageReference.getDownloadUrl().addOnSuccessListener()。使用之前,您应该检查url是否为空

String url;
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                Uri dlUri = uri;
                url =dlUri.toString();
            }
        });

答案 2 :(得分:0)

您无需在onSsuccesslistener中再次调用storageReference.getDownloadUrl(),您可以直接以

的形式获取网址
  

taskSnapshot.downloadUrl.toString()

请参阅下面的代码。

storageReference.putFile(selected).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

    @SuppressWarnings("VisibleForTests")
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
              String url = taskSnapshot.downloadUrl.toString()
       } }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Toast.makeText(getApplicationContext(), e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
    }
});

   }