Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将文件从Firebase存储下载到Android的外部_存储_Android_Firebase_Firebase Storage - Fatal编程技术网

如何将文件从Firebase存储下载到Android的外部_存储

如何将文件从Firebase存储下载到Android的外部_存储,android,firebase,firebase-storage,Android,Firebase,Firebase Storage,我有一个PDF和一些文档文件保存在firebase存储中 如何将文件从Firebase存储下载到设备的外部存储 public void writeExternalStorage() { String filename; String completepath; mref = FirebaseStorage.getInstance().getReference(); StorageReference filepath = mref.child("MyFiles").

我有一个PDF和一些文档文件保存在firebase存储中

如何将文件从Firebase存储下载到设备的外部存储

public void writeExternalStorage() {
    String filename;
    String completepath;
    mref = FirebaseStorage.getInstance().getReference();
    StorageReference filepath = mref.child("MyFiles").child("firstFile.pdf");
    InputStream inputStream = null;
    File file = null;
    FileOutputStream fileOutputStream = null;
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        try {
            file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath() + "/TestPurpose");
            //  Log.d("PATH", file.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
我想将Firstfile.pdf文件下载到外部存储的Document/TestPurpose/文件夹中。如何做到这一点???

私有void downloadFile(){
    private void downloadFile() {
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReferenceFromUrl("<your_bucket>");
    StorageReference  islandRef = storageRef.child("file.txt");

    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());
            //  updateDb(timestamp,localFile.toString(),position);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Log.e("firebase ",";local tem file not created  created " +exception.toString());
        }
    });
}
FirebaseStorage=FirebaseStorage.getInstance(); StorageReference-storageRef=storage.getReferenceFromUrl(“”); StorageReference islandRef=storageRef.child(“file.txt”); File rootPath=新文件(Environment.getExternalStorageDirectory(),“文件名”); 如果(!rootPath.exists()){ rootPath.mkdirs(); } 最终文件localFile=新文件(rootPath,“imageName.txt”); islandRef.getFile(localFile).addOnSuccessListener(新的OnSuccessListener(){ @凌驾 成功时公共无效(FileDownloadTask.TaskSnapshot TaskSnapshot){ Log.e(“firebase”,“创建本地tem文件”+localFile.toString()); //updateDb(时间戳,localFile.toString(),位置); } }).addOnFailureListener(新的OnFailureListener(){ @凌驾 public void onFailure(@NonNull异常){ Log.e(“firebase”,“未创建本地tem文件”+exception.toString()); } }); }
MainActivity.java

 Button button=findViewById(R.id.download);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             downloadfile();
        }
    });
}

private void downloadfile() {

     FirebaseStorage storage = FirebaseStorage.getInstance();
     StorageReference storageRef = storage.getReferenceFromUrl("https://firebasestorage.googleapis.com/v0/b/imagestore-b2432.appspot.com/o/Nature.jpg?alt=media&token=07d95162-45f8-424e-9658-8f9022485930");

    ProgressDialog  pd = new ProgressDialog(this);
    pd.setTitle("Nature.jpg");
    pd.setMessage("Downloading Please Wait!");
    pd.setIndeterminate(true);
    pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    pd.show();


    final File rootPath = new File(Environment.getExternalStorageDirectory(), "MADBO DOWNLOADS");

    if (!rootPath.exists()) {
        rootPath.mkdirs();
    }


    final File localFile = new File(rootPath, "Nature.jpg");

    storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener <FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            Log.e("firebase ", ";local tem file created  created " + localFile.toString());

            if (!isVisible()){
                   return;
               }

               if (localFile.canRead()){

                pd.dismiss();
            }

            Toast.makeText(this, "Download Completed", Toast.LENGTH_SHORT).show();
            Toast.makeText(this, "Internal storage/MADBO/Nature.jpg", Toast.LENGTH_LONG).show();

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Log.e("firebase ", ";local tem file not created  created " + exception.toString());
            Toast.makeText(this, "Download Incompleted", Toast.LENGTH_LONG).show();
        }
    });
}
Button-Button=findviewbyd(R.id.download);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
下载文件();
}
});
}
私有void下载文件(){
FirebaseStorage=FirebaseStorage.getInstance();
StorageReference storageRef=storage.getReferenceFromUrl(“https://firebasestorage.googleapis.com/v0/b/imagestore-b2432.appspot.com/o/Nature.jpg?alt=media&token=07d95162-45f8-424e-9658-8f9022485930“;
ProgressDialog pd=新的ProgressDialog(本);
pd.setTitle(“Nature.jpg”);
设置消息(“正在下载,请稍候!”);
pd.SetUndeterminate(真);
pd.setProgressStyle(ProgressDialog.STYLE_微调器);
pd.show();
最终文件根路径=新文件(Environment.getExternalStorageDirectory(),“MADBO下载”);
如果(!rootPath.exists()){
rootPath.mkdirs();
}
最终文件localFile=新文件(rootPath,“Nature.jpg”);
storageRef.getFile(localFile).addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时公共无效(FileDownloadTask.TaskSnapshot TaskSnapshot){
Log.e(“firebase”,“创建本地tem文件”+localFile.toString());
如果(!isVisible()){
返回;
}
if(localFile.canRead()){
pd.解散();
}
Toast.makeText(这是“下载完成”,Toast.LENGTH_SHORT).show();
Toast.makeText(这是“Internal storage/MADBO/Nature.jpg”,Toast.LENGTH_LONG.show();
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常){
Log.e(“firebase”,“未创建本地tem文件”+exception.toString());
Toast.makeText(这是“下载未完成”,Toast.LENGTH_LONG.show();
}
});
}
AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>


您不了解下载或保存文件的哪一部分?标记为太宽--您需要查看并尝试这些内容。一旦您启用了这些功能,您应该能够使用指向外部存储的文件URI,一切都应该正常工作™这对我很有用:这真的很有帮助!但是,如果我想更改上传到Firebase的每个图像的名称,该怎么办?我可以在这一行添加任何内容,以便每一个文件都能不断更改吗<代码>文件localFile=新文件(rootPath,“imageName.txt”)上载文件时,在所需的属性中设置文件名。然后检索文件名并传入参数。下载响应时,无论是成功还是失败都不要进入。。有什么问题?@Makvin检查firebase存储规则