如何从Firebase存储获取URL并将其发送到数据库?

时间:2020-05-04 23:19:18

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

我正在尝试将图像上传到Firebase存储,然后将该图像的URL发送到Firebase数据库。 Uri是正确的,但是当我尝试在对象上设置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();
                            }
                        });
                    }
                });

1 个答案:

答案 0 :(得分:0)

您正在正确上传图像,但是使用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。