FirebaseStorageAPI is an Android Library wrapper for Firebase Storage functionality in android with multiple features
Features
- 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
how to install
Step 1:Setup Firebase Storage
Add Firebase Storage library to your Firebase project https://firebase.google.com/docs/storage/android/start
Step 2:Add Gradle dependency
- 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'
. . .
}
Usage
Step 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, try later")
.setUploadingMessage("carrying it to cloud")
.setLoadingMessage("waiting to start")
.allowCancel(true) //Download can be cancelled
.build();
Step 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");
Step 3:now we are ready to upload or download files
A- Upload files
1- prepare the data as one of example below:
InputStream DataToUpload = ...; // data as inputstream
Uri DataToUpload = Uri.fromFile(new File("/sdcard/hello.txt")); //data as Uri
String string = "helloWorldInBytes"; byte[] DataToUpload=string.getBytes(); // data as Bytes
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
- you can download it as various types of data as:
— 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
}
}
});
That is all.