从Firebase-Storage将图像存储在本地存储中并获取路径

时间:2019-07-20 12:42:05

标签: android local-storage firebase-storage

我将图像存储在Firebase存储器中。现在,我想从存储中获取图像,将其下载到本地存储中,并保存路径以备将来使用。

我尝试过的是:

FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
            StorageReference httpsReference = firebaseStorage.getReferenceFromUrl("gs://****.appspot.com/image_pics/" + userID + ".jpg");

//images are saved with named based on the userid of the user plus an extension of jpg

现在,我想使用httpsReference将图像下载到本地存储中并返回路径(以字符串形式)。

我不知道我该使用Firebase数据库中的存储位置(即gs // ...)还是URL。 请让我知道。

1 个答案:

答案 0 :(得分:2)

此代码将帮助您将文件保存在本地存储中

FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
StorageReference httpsReference = firebaseStorage.getReferenceFromUrl("gs://****.appspot.com/image_pics/" + userID + ".jpg"); // IF this is working for you

StorageReference  myStorage = storageRef.child("file.txt"); // Use this and provide the reference
File rootPath = new File(Environment.getExternalStorageDirectory(), "file_name");
        if(!rootPath.exists()) {
            rootPath.mkdirs();
        }
final File localFile = new File(rootPath,"imageName.txt");
islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
        Log.e("firebase local tem file created  created " + localFile.toString());
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        Log.e("firebase ",";local tem file not created  created " +exception.toString());
    }
});