无法从Firebase存储获取图像URL

时间:2020-04-29 17:29:57

标签: android firebase firebase-storage

我正在尝试将图像上传到Firebase存储,然后将该图像的URL发送到Firebase数据库。当我上传图像而没有获取URL时,一切正常,但是当我尝试获取URL时,图像甚至都没有上传。 这是我的代码:

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) {
                        //This toast is showing when I comment the part of code with getting URL
                        Toast.makeText(getApplicationContext(), "Successfully sent a file", Toast.LENGTH_LONG).show();
                        Task<Uri> downloadUri = taskSnapshot.getStorage().getDownloadUrl();
                        if (downloadUri.isSuccessful()){
                            String imagePath = downloadUri.getResult().toString();
                            singleDetection.setImage(imagePath);
                        }
                        else{
                            //This Toast is showing when I run the code
                            Toast.makeText(getApplicationContext(), "Failed to get an URL", Toast.LENGTH_LONG).show();
                        }
                    }
                });

编辑: 我已经更改了代码,现在看起来像这样:

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();
                            }
                        });
                    }
                });

现在我还有另一个问题。 Uri是正确的,但是当我尝试在对象上设置Uri时,方法singleDetection.setImage(imagePath)没有设置任何内容。

0 个答案:

没有答案