Media files download from firebase using android service
File download from firebase storage using android service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Global declaration for Firebase Storage reference | |
private StorageReference mStorageRef; | |
// Initialize Storage in onCreate method of service | |
mStorageRef = FirebaseStorage.getInstance().getReference(); | |
/** | |
* Download file using firebase path | |
* @param downloadPath - Firebase location path | |
* @param localPath - Local storage path | |
*/ | |
private void downloadFromPath(final String downloadPath, String localPath) { | |
Log.d(TAG, "downloadFromPath:" + downloadPath); //Download firebase path | |
//Initialize progress notification | |
showProgressNotification(getString(R.string.progress_downloading), 0, 0); | |
mStorageRef = mStorageRef.child(downloadPath); | |
File file = new File(localPath); // Local file location for firebase file download | |
mStorageRef.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { | |
@Override | |
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { | |
String path = String.valueOf(file); | |
Log.e("download_path", "" + path); | |
Log.d(TAG, "download:SUCCESS" + downloadPath); | |
// Send success broadcast with number of bytes downloaded | |
broadcastDownloadFinished(downloadPath, taskSnapshot.getTotalByteCount()); | |
showDownloadFinishedNotification(downloadPath, (int) taskSnapshot.getTotalByteCount()); | |
stopSelf(); | |
} | |
}).addOnFailureListener(new OnFailureListener() { | |
@Override | |
public void onFailure(Exception exception) { | |
Log.e("exception", "" + exception.getMessage()); | |
Log.w(TAG, "download:FAILURE", exception); | |
// Send failure broadcast to your activity | |
broadcastDownloadFinished(downloadPath, -1); | |
showDownloadFinishedNotification(downloadPath, -1); | |
stopSelf(); | |
} | |
}).addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() { | |
@Override | |
public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) { | |
//Show progress notification with precentage | |
showProgressNotification(getString(R.string.progress_downloading), taskSnapshot.getBytesTransferred(), taskSnapshot.getTotalByteCount()); | |
} | |
}); | |
} |
Comments
Post a Comment