Muhammad Sohail
Muhammad Sohail

Reputation: 13

Download audio from Firebase storage

I'm trying to create a voice messaging android app using firebase storage as backend, but i'm having issue while downloading mp3 file while it uploads successfully.

Can somebody please provide simple code just to download an audio file from storage. I'm new at android development , so sorry for being noob

i've added all the permissions in my manifest file like bellow

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

Tis is my uploading code

private void uploadAudio() {
    pd=new ProgressDialog(this);
    pd.setMessage("Uploading");
   pd.show();
    StorageReference filepath = mstorage.child("Audio").child("New_Audio");
    Uri uri = Uri.fromFile(new File(mFileName));
    filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
          pd.dismiss();


        }
    });
}

And for downloading

private void startdownload() {

    down = mstorage.child("Audio/");

    File localFile = null;
    try {
        localFile = File.createTempFile("Audio", "mp3");
    } catch (IOException e) {
        e.printStackTrace();
    }

    down.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            Toast.makeText(getApplicationContext(),"Downloded",Toast.LENGTH_SHORT).show();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {

        }
    });
}

Upvotes: 1

Views: 7088

Answers (2)

Rashid Hussain
Rashid Hussain

Reputation: 54

For Downloading use :

down = mstorage.child("Audio").child("New_Audio.mp3");

Once the file is downloaded you can check the file path by

localFile.getAbsolutePath();

Make a Toast of it, Like this :

Toast.makeText(getApplicationContext(), localFile.getAbsolutePath(),Toast.LENGTH_SHORT).show();

I hope You got my point :)

Upvotes: 0

Wilik
Wilik

Reputation: 7720

Make sure your StorageReference for the upload and download method is the same.

//upload
StorageReference filepath = mstorage.child("Audio").child("New_Audio.mp3");
//download
down = mstorage.child("Audio").child("New_Audio.mp3");

To save the file in the external storage, you can follow this Android Developers guide

An example:

try {
    File localFile  = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS), "New_Audio.mp3");
    localFile .createNewFile();
    down.getFile(localFile);
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 3

Related Questions