繁体   English   中英

获取从Firebase下载的图片的路径?

[英]Get path from picture downloaded from Firebase?

我正在开发一个应用程序,用户可以在其中拍摄和上传图片,而另一个用户可以在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());
            }

        }

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM