Android Library wrapper for Firebase Storage functionality.
FirebaseStorageAPI is an Android Library wrapper for Firebase Storage functionality.
Add Firebase Storage library to your Firebase project
https://firebase.google.com/docs/storage/android/start
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
## Usage
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](https://firebase.google.com/docs/storage/android/create-reference)
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
@Override
public void onSuccess(Uri ) {
statusTextView.setText("file uploaded successfully\n File URL: " + uri.toString());
}}); }} });
**B- Download function**
- InputStream
firebaseStorageAPI.downloadAsStream(mStorageRef, new OnCompleteListener
@Override
public void onComplete(@NonNull final 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
@Override
public void onComplete(@NonNull Task
if (task.isComplete()) {
// download is complete, do your thing with the localFile …
}
}
});
- Bytes
firebaseStorageAPI.downloadAsBytes(mStorageRef, Long.MAX_VALUE, new OnCompleteListener
@Override
public void onComplete(@NonNull Task
if (task.isComplete()) {
byte[] dataInBytes = task.getResult();
// download complete , do your thing with the dataInBytes
}
}
});
~~~
MIT © mshlab.