FirebaseStorageAPI is an Android Library wrapper for Firebase Storage functionality.
- built-in Progress Dialog
- customizable messages fit your need and language
- Upload and download files to Firebase Storage Bucket in different forms like:
- stream as an IntputStream
- Array of Bytes
- File on Device Storage
- Delete files
Add Firebase Storage library to your Firebase project https://firebase.google.com/docs/storage/android/start
- Add the following to your project level build.gradle:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
- Add this to your app build.gradle:
dependencies {
...
implementation 'com.github.mshlab:FirebaseStorageAPI:v1.0-release'
}
1- create a FirebaseStorageAPI object
FirebaseStorageAPI firebaseStorageAPI = new FirebaseStorageAPI.Builder() .setVisibleAcitivty(this) //required .build();
- add more custom options
firebaseStorageAPI = new FirebaseStorageAPI.Builder() .setVisibleAcitivty(this) //required .setCancelMessage("file download canceled") .setDownloadingMessage("downloading from the sky")
.setErrorMessage("error in gating the file, try later") .setUploadingMessage("carrying it to cloud")
.setLoadingMessage("waiting to start")
.allowCancel(true) //let the user choose to cancel the download .build();
2- defined a storage reference (path where you will upload or download your file) learn how
StorageReference mStorageRef = FirebaseStorage.getInstance().
getReference().
getRoot().
child("pics").
child("sky.png");
A- Upload function
1- prepare the data as :
- inputstream
InputStream DataToUpload = ...; - File
Uri DataToUpload = Uri.fromFile(new File("/sdcard/hello.txt"));
- Bytes
String string = "helloWorldInBytes";
byte[] DataToUpload=string.getBytes();
2- pass it to upload method
firebaseStorageAPI.upload(DataToUpload, mStorageRef, new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (task.isSuccessful()) { mStorageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri ) { statusTextView.setText("file uploaded successfully\n File URL: " + uri.toString()); }}); }} }); B- Download function
- InputStream
firebaseStorageAPI.downloadAsStream(mStorageRef, new OnCompleteListener<StreamDownloadTask.TaskSnapshot>() { @Override public void onComplete(@NonNull final Task<StreamDownloadTask.TaskSnapshot> task) { if (task.isComplete()) { InputStream is = task.getResult().getStream(); //InputStream is ready to use } } });
- File
File localFile = File.createTempFile("images", "jpg"); firebaseStorageAPI.downloadToLocalPath(mStorageRef, localFile, new OnCompleteListener<FileDownloadTask.TaskSnapshot>() { @Override public void onComplete(@NonNull Task<FileDownloadTask.TaskSnapshot> task) { if (task.isComplete()) { // download is complete, do your thing with the localFile ... } }
});
- Bytes
firebaseStorageAPI.downloadAsBytes(mStorageRef, Long.MAX_VALUE, new OnCompleteListener<byte[]>({ @Override public void onComplete(@NonNull Task<byte[]> task) { if (task.isComplete()) {
byte[] dataInBytes = task.getResult(); // download complete , do your thing with the dataInBytes
} } });
MIT © mshlab.

