简体   繁体   中英

How to download file into internal downloads folder in Android

I am pretty new with this, and I could use some help. I am having difficulties downloading files from firebase storage into my android device storage. Problem is that files are downloaded into cache memory of app, and I would like to use them when I'm offline. Other thing is that I am not using SD card, so I would like my files downloaded into some of user accessible folder, if possible into download folder in internal memory. Here is my code:

StorageReference ref = storage.getReferenceFromUrl("gs://diplomskirad-948a7.appspot.com/Dennis Lloyd - NEVERMIND.mp3");
    File localFile=null;
    try {
        localFile = File.createTempFile("audio", ".mp3");
    } catch (IOException e) {
        e.printStackTrace();
    }
    ref.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            Log.d("Success", "File downloaded successfully");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Log.e("Error", "Error downloading file");
        }
    });

I guess should somehow change my "onSuccess" method, but I didn't have any success until now.

Try this:-

StorageReference ref = storage.getReferenceFromUrl("gs://diplomskirad-948a7.appspot.com/Dennis Lloyd - NEVERMIND.mp3");

  File path= new File(Environment.getExternalStorageDirectory(), "file_name");
    if(!path.exists()) {
        path.mkdirs();
    }

   final File localFile = new File(path,"audio.mp3");

    ref.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            Log.d("Success", "File downloaded successfully " + localFile.toString());
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Log.e("Error", "Error downloading file");
        }
    });

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