【问题标题】:Get path from picture downloaded from Firebase?从 Firebase 下载的图片获取路径?
【发布时间】:2017-03-29 15:13:08
【问题描述】:

我正在制作一个应用程序,用户可以在其中拍摄和上传照片,而另一个用户可以在ImageView 中下载和查看它。为了防止我占用应用程序的所有内存,我将整个图片存储在存储空间中。

它已与此处的标准公式一起使用; https://firebase.google.com/docs/storage/android/download-files

问题是,我不知道如何获取图像下载到的路径。我在 AVD 监视器中找不到它,我需要每个下载图片的 URL,因为它将是自定义的。我尝试使用这样的东西,它会创建一个新目录并将其放在那里,但它不起作用,我也找不到路径。

public void getImageFromPath() throws IOException {
            StorageReference islandRef = storageRef.child("userimages/test@test.dk/test@test.dk");

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

            String imageFileName = "JPEG_" + timeStamp + "_";

            File directory = new File(Environment.getDataDirectory()
                    + "/Test/");
            if (!directory.exists()) {
                directory.mkdir();
            }
            final File localFile = File.createTempFile(imageFileName, ".jpg", directory);
            try {
                islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                        Toast.makeText(getApplicationContext(), "Downloaded", Toast.LENGTH_SHORT).show();
                        Log.d(TAG, "Downloaded til path");
                        String mCurrentPhotoPath = "file:" + localFile.getAbsolutePath();
                        System.out.println(mCurrentPhotoPath);

                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        Log.d(TAG, "Downloaded IKKE til path");
                    }
                });
                throw new IOException("Works not");
            }
            catch(IOException e)
            {
                System.out.println(e.getMessage());
            }

        }

【问题讨论】:

    标签: android firebase firebase-storage


    【解决方案1】:

    您正在创建一个临时本地文件。这是您将数据下载到的位置。

    然后您可以使用以下命令获取该文件的绝对路径:

    File localFile = File.createTempFile(imageFileName, ".jpg", directory);
    String path = localFile.getAbsolutePath();
    

    【讨论】:

    • 感谢您的回复!不过,我现在似乎收到“打开失败:ENOENT(没有这样的文件或目录)”异常。 :( 它出现在这些行中你的字符串路径之前;最终文件 localFile = File.createTempFile(imageFileName, ".jpg", directory); String path = localFile.getAbsolutePath(); System.out.println("test" +路径);
    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 2017-03-10
    • 2012-06-05
    • 2019-10-04
    • 1970-01-01
    • 2015-10-21
    相关资源
    最近更新 更多