【问题标题】:Can not get download URL for uploaded image to firebase android [duplicate]无法获取上传图像到firebase android的下载URL [重复]
【发布时间】:2020-11-02 16:41:58
【问题描述】:

我有这个代码,它通过单击按钮从 imageview 上传图像。即使此代码上传了图像,我也无法获取图像的下载 url,我可以在将来的参考目的中使用。请帮助我获取下载 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();

                    }

                });


            }
        });

【问题讨论】:

  • 确定下载 URL 需要调用服务器。因此,对getDownloadUrl() 的调用返回一个Task,当下载URL 从服务器返回时完成。您需要调用 addSuccessListener() 以等待它完成。请参阅文档here 和此answer

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


【解决方案1】:

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

经过我的搜索,有一个叫AsyncTask的东西,它的工作方法是覆盖需要更长时间的订单并执行它之后的订单,完成后返回它

解决这个问题你可以使用这个方法来获取downloadUrl的值

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

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

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

我希望我为你简化了一些事情并帮助你。

【讨论】:

    猜你喜欢
    • 2020-10-31
    • 2020-01-29
    • 2018-01-24
    • 2021-07-17
    • 2020-05-30
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 2020-12-05
    相关资源
    最近更新 更多