无法获取将上传的图片下载到Firebase Android的下载URL

时间:2020-07-13 13:14:52

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

我有这段代码,单击按钮即可从imageview上传图像。即使通过此代码上传了图像,我也无法获得该图像的下载URL,我可以将其用于将来的参考目的。请帮助我获得下载网址。仅供参考,getDownloadURl()函数已被弃用,无法正常工作。谢谢!

    Button uploadBtn = findViewById(R.id.upload_btn);
            uploadBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FirebaseStorage storage = FirebaseStorage.getInstance();
                    // Create a storage reference from our app
                    StorageReference storageRef = storage.getReferenceFromUrl("gs://'''''''.appspot.com/");
    
    // Create a reference to "mountains.jpg"
                    StorageReference mountainsRef = storageRef.child(System.currentTimeMillis() + ".jpg");
    
    // Create a reference to 'images/mountains.jpg'
                    StorageReference mountainImagesRef = storageRef.child("images" + System.currentTimeMillis() + ".jpg");

    // While the file names are the same, the references point to different files
                    mountainsRef.getName().equals(mountainImagesRef.getName());    // true
                    mountainsRef.getPath().equals(mountainImagesRef.getPath());    // false

                imageView.setDrawingCacheEnabled(true);
                imageView.buildDrawingCache();
                Bitmap bitmap = imageView.getDrawingCache();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] data = baos.toByteArray();

                UploadTask uploadTask = mountainsRef.putBytes(data);
                uploadTask.addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        // Handle unsuccessful uploads
                    }
                }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                        String downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
                        Toast.makeText(ClickImage.this, downloadUrl, Toast.LENGTH_SHORT).show();

                    }

                });


            }
        });

1 个答案:

答案 0 :(得分:0)

我之前遇到过这个问题,谁可以正确下载Firebase,但不能正确下载 downloadUrl 中的链接。

在我搜索此问题之后,有一个名为 AsyncTask 的事物,其工作方法可以覆盖需要较长时间的订单,并在之后执行订单它并在完成后返回它

解决此问题,您可以使用此方法来检索 downloadUrl

的值
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    storageReference.getDownloadUrl().addOnCompleteListener(task -> {

                        String downloadUrl = Objects.requireNonNull(task.getResult()).toString();
    
                    });

希望我能为您简化事情并为您提供帮助。