web-dev-qa-db-ja.com

Firebase StorageからAndroidのExternal_Storageにファイルをダウンロードする方法

PDFといくつかのdocファイルをfirebaseストレージに保存しています。

Firebase Storageからデバイスの外部ストレージにファイルをダウンロードするにはどうすればよいですか?

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ファイルをExternal_storageのDocument/TestPurpose /フォルダーにダウンロードします。どうすればできますか?

5
Sid
    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());
        }
    });
}
17
Stephenraj

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();
        }
    });
}

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"/>
0
Mahesh Bokhani