Media files download from firebase using android service

File download from firebase storage using android service

//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

Popular posts from this blog

Your build is currently configured to use incompatible Java 21.0.3 and Gradle 8.2.1. Cannot sync the project.

Google Assistant Implementation in Android application with app actions