简体   繁体   中英

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) {

        }
    });
}

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

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM