【问题标题】:How to get URL from Firebase Storage and send it to database? [duplicate]如何从 Firebase Storage 获取 URL 并将其发送到数据库? [复制]
【发布时间】:2020-08-19 12:42:12
【问题描述】:

我正在尝试将图像上传到 Firebase 存储,然后将此图像的 URL 发送到 Firebase 数据库。 Uri 是正确的,但是当我尝试在我的对象上设置它时,方法 singleDetection.setImage(imagePath) 没有设置任何内容。这是我的代码:

Bitmap image = detectedFaceItems.get(0).getImage();
        StorageReference storageRef = storage.getReference();
        StorageReference imagesRef = storageRef.child("2.jpg");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] data = baos.toByteArray();
        StorageTask<UploadTask.TaskSnapshot> uploadTask = imagesRef.putBytes(data)
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(getApplicationContext(), "Failed to sent a file", Toast.LENGTH_LONG).show();
                    }
                })
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Toast.makeText(getApplicationContext(), "Successfully sent a file", Toast.LENGTH_LONG).show();
                        storageRef.child("2.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                //Toast.makeText(getApplicationContext(), uri.toString(), Toast.LENGTH_LONG).show();
                                //Doesn't work corretly
                                String imagePath = uri.toString();
                                singleDetection.setImage(imagePath);
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(getApplicationContext(), "Failed to get an Uri", Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                });

【问题讨论】:

  • 我不确定singleDetection.setImage(imagePath) 应该做什么。但是如果你Log.i("URL", imagePath),你的 logcat 中会输出什么?
  • setImage 只是将字符串设置为对象。 onSuccess 方法内部的 Log.i("URL", imagePath) 和 Log.i("URL", singleDetection.getImage()) 正确返回 url,但外部的 Log.i("URL", singleDetection.getImae()) onSuccess 方法返回 nullPointerException。
  • 这是预期的:由于下载 URL 是从服务器检索的,因此需要访问下载 URL 的任何代码都需要在 onSuccess 内或从那里调用。见stackoverflow.com/questions/53127996/…

标签: android firebase firebase-realtime-database firebase-storage


【解决方案1】:

您正在正确上传图片,但是在使用 storageRef.child("2.jpg").getDownloadUrl(). 成功上传图片后您对 Firebase 的调用不是正确的,因为您已经不知道生成的 Uri,您可以从返回的快照中获取taskSnapshot.

所以替换下面部分代码:

.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        Toast.makeText(getApplicationContext(), "Successfully sent a file", Toast.LENGTH_LONG).show();
        storageRef.child("2.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                //Toast.makeText(getApplicationContext(), uri.toString(), Toast.LENGTH_LONG).show();
                //Doesn't work corretly
                String imagePath = uri.toString();
                singleDetection.setImage(imagePath);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getApplicationContext(), "Failed to get an Uri", Toast.LENGTH_LONG).show();
            }
        });
    }
});

与:

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

        // get upload url
        taskSnapshot.getStorage().getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                String imagePath = String.valueOf(task.getResult());
                singleDetection.setImage(imagePath);

            }
        });
    }
});

注意:这里您已经上传了图片本身,但没有上传它的 Uri,因此您可以根据需要决定将其上传到 firebase。

【讨论】:

  • 还是同样的问题。在 onComplete 方法中,imagePath 看起来不错,但在此方法之外它为 null。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 2020-09-22
  • 2019-10-10
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多