Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a file from Firebase Storage to the External_Storage of Android

I have a PDF and some doc files kept in firebase storage.

How can I download the file from Firebase Storage to the External storage of my device?

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

I want to download the Firstfile.pdf file to the External_storage's Document/TestPurpose/ Folder. How it can be done??

like image 223
Sid Avatar asked Oct 06 '16 21:10

Sid


People also ask

How do I download from Firebase Storage to Android?

To download a file, first create a Cloud Storage reference to the file you want to download. You can create a reference by appending child paths to the root of your Cloud Storage bucket, or you can create a reference from an existing gs:// or https:// URL referencing an object in Cloud Storage.

How do I retrieve files from Firebase Storage?

There are two ways to download files with Firebase Storage, using references in the SDK or a download URL. iOS and Android users can download files into memory, disk, or from a download URL. The web SDK can download files just from a download URL. StorageReference storageRef = FirebaseStorage.


2 Answers

    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());
        }
    });
}
like image 89
Stephenraj Avatar answered Sep 29 '22 23:09

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"/>
like image 20
Mahesh Bokhani Avatar answered Sep 29 '22 22:09

Mahesh Bokhani