项目作者: m4ary

项目描述 :
Android Library wrapper for Firebase Storage functionality.
高级语言: Java
项目地址: git://github.com/m4ary/FirebaseStorageAPI.git
创建时间: 2020-05-15T22:30:24Z
项目社区:https://github.com/m4ary/FirebaseStorageAPI

开源协议:MIT License

下载


FirebaseStorageAPI

FirebaseStorageAPI is an Android Library wrapper for Firebase Storage functionality.
screenshot

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


Download

1- Setup Firebase Storage

Add Firebase Storage library to your Firebase project
https://firebase.google.com/docs/storage/android/start

2-Add Gradle dependency

  • Add the following to your project level build.gradle:
    1. allprojects {
    2. repositories {
    3. ...
    4. maven { url "https://jitpack.io" }
    5. }
    6. }
  • Add this to your app build.gradle:
    ~~~
    dependencies {

    implementation ‘com.github.mshlab:FirebaseStorageAPI:v1.0-release’
    }
  1. ## Usage
  2. 1- create a FirebaseStorageAPI object
  1. FirebaseStorageAPI firebaseStorageAPI = new FirebaseStorageAPI.Builder()
  2. .setVisibleAcitivty(this) //required
  3. .build();
  1. - add more custom options
  1. firebaseStorageAPI = new FirebaseStorageAPI.Builder()
  2. .setVisibleAcitivty(this) //required
  3. .setCancelMessage("file download canceled")
  4. .setDownloadingMessage("downloading from the sky")
  5. .setErrorMessage("error in gating the file, try later")
  6. .setUploadingMessage("carrying it to cloud")
  7. .setLoadingMessage("waiting to start")
  8. .allowCancel(true) //let the user choose to cancel the download
  9. .build();
  1. 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”);

  1. **A- Upload function**
  2. 1- prepare the data as :
  3. - inputstream

InputStream DataToUpload = …;

  1. - File

Uri DataToUpload = Uri.fromFile(new File(“/sdcard/hello.txt”));

  1. - Bytes

String string = “helloWorldInBytes”;
byte[] DataToUpload=string.getBytes();

  1. 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() {

  1. @Override
  2. public void onSuccess(Uri ) {
  3. statusTextView.setText("file uploaded successfully\n File URL: " + uri.toString());

}}); }} });

  1. **B- Download function**
  2. - InputStream

firebaseStorageAPI.downloadAsStream(mStorageRef, new OnCompleteListener() {
@Override
public void onComplete(@NonNull final Task task) {

if (task.isComplete()) {
InputStream is = task.getResult().getStream();
//InputStream is ready to use
}
}
});

  1. - File

File localFile = File.createTempFile(“images”, “jpg”);
firebaseStorageAPI.downloadToLocalPath(mStorageRef, localFile, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isComplete()) {
// download is complete, do your thing with the localFile …
}
}
});

  1. - Bytes

firebaseStorageAPI.downloadAsBytes(mStorageRef, Long.MAX_VALUE, new OnCompleteListener({
@Override
public void onComplete(@NonNull Task task) {
if (task.isComplete()) {
byte[] dataInBytes = task.getResult();
// download complete , do your thing with the dataInBytes
}
}
});
~~~


Buy me a coffee

Buy Me A Coffee

License

MIT © mshlab.