在Android中,从Firebase存储下载的文件存储在哪里? [英] In Android, where does the file downloaded from Firebase storage get stored?

查看:55
本文介绍了在Android中,从Firebase存储下载的文件存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从项目控制台将文件上传到Firebase Storage.现在,我使用Firebase存储文档中提到的"下载到本地文件"方法从我的应用下载了该文件,但是我无法在手机上找到下载的文件.

有人可以告诉我该文件下载到哪里吗?

    FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
    final StorageReference storageReference = firebaseStorage.getReferenceFromUrl("gs://ldq-app-d2e6b.appspot.com");

    button_down.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String fileName = new String(editText_file.getText().toString());

            StorageReference childReference = storageReference.child("Quizzes");

            StorageReference fileReference = storageReference.child("Quizzes/" + fileName + ".pdf");

            File localFile = null;

            try {
                localFile = File.createTempFile(fileName, "pdf");
            }
            catch (IOException ioe){
                Toast.makeText(getContext(), "File creation failed", Toast.LENGTH_SHORT).show();
            }

            fileReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(getContext(),"File downloaded",LENGTH_SHORT).show();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getContext(),"Download failed. Try again!", LENGTH_SHORT).show();
                }
            });
            //Toast.makeText(getContext(),"Button working",LENGTH_SHORT).show();
        }
    });

解决方案

根据Firebase文档,他们正在创建一个临时文件以从网址"保存图片. 链接

File localFile = File.createTempFile("images", "jpg");

如果要将其存储在手机的内部存储器或外部存储器中,则只需创建一个具有存储路径的文件,然后将其传递给 FileDownloadTask .

File storagePath = new File(Environment.getExternalStorageDirectory(), "directory_name");
// Create direcorty if not exists
if(!storagePath.exists()) {
    storagePath.mkdirs();
}

final File myFile = new File(storagePath,"file_name");

yourStorageRef.getFile(myFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
    // Local temp file has been created
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
    // Handle any errors
}
});

I uploaded a file to Firebase Storage from my project console. Now, I downloaded that file from my app using the method 'Download to a local file' as mentioned in the Firebase Storage documentation, but I am not able to find the downloaded file on my phone.

Can someone tell me where does that file get downloaded to?

    FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
    final StorageReference storageReference = firebaseStorage.getReferenceFromUrl("gs://ldq-app-d2e6b.appspot.com");

    button_down.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String fileName = new String(editText_file.getText().toString());

            StorageReference childReference = storageReference.child("Quizzes");

            StorageReference fileReference = storageReference.child("Quizzes/" + fileName + ".pdf");

            File localFile = null;

            try {
                localFile = File.createTempFile(fileName, "pdf");
            }
            catch (IOException ioe){
                Toast.makeText(getContext(), "File creation failed", Toast.LENGTH_SHORT).show();
            }

            fileReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(getContext(),"File downloaded",LENGTH_SHORT).show();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getContext(),"Download failed. Try again!", LENGTH_SHORT).show();
                }
            });
            //Toast.makeText(getContext(),"Button working",LENGTH_SHORT).show();
        }
    });

解决方案

As per Firebase Document, they are creating a temporary file for saving Image from Url. Link

File localFile = File.createTempFile("images", "jpg");

If you want to store it on phone's internal memory or in external memory then just create a File with storage path and pass it to FileDownloadTask.

File storagePath = new File(Environment.getExternalStorageDirectory(), "directory_name");
// Create direcorty if not exists
if(!storagePath.exists()) {
    storagePath.mkdirs();
}

final File myFile = new File(storagePath,"file_name");

yourStorageRef.getFile(myFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
    // Local temp file has been created
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
    // Handle any errors
}
});

这篇关于在Android中,从Firebase存储下载的文件存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆