[Android Software Development] Geeks Empire:
Google Firebase.Storage

​

It s time to do something important with Cloud Service; Upload & Download File.

Google Cloud Service (Firebase) has lots of Useful Features + Really Straight & Manageable API.

First Read Preface Post & Firebase Basics to Prepare your Project for Cloud Services.

Now you will Understand upcoming codes.

Inside build.gradle of AppLevel declare Firebase.Storage SDK in dependencies

compile 'com.google.firebase:firebase-[B]storage[/B]:9.0.2'
compile 'com.google.firebase:firebase-[B]auth[/B]:9.0.2'

In update Sample Code I created new activity to handle Files. You can do whatever you like.

NOTE: You have to Authenticate Users with Firebase Services to Perform any Server action. For example Uploading File. check out SourceCode

In activity onCreate() declare instance of Firebase.Storage & Firebase.Auth

firebaseStorage = [B]FirebaseStorage[/B].getInstance();
firebaseAuth = [B]FirebaseAuth[/B].getInstance();

To Perform Both Upload & Download Task you need to create a Reference.

There is different method for creating StorageReference that I chose:

getReferenceFromUrl() for Uploading
& getFile() for Downloading.

Before anything do Auth Process. As Public APP I used Anonymously option & call the function when Activity Start.

private void signInAnonymously() {
mAuth.signInAnonymously().addOnSuccessListener(this, new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {}
}).addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {}
});
}

Now you can Add StorageReference & Perform Actions

UPLOAD Function check out SourceCode

private void uploadFromUri(InputStream fileUri) {
StorageReference mStorageRef = storage.getReferenceFromUrl("gs://[I][COLOR=Red][B][YOUR_FIREBASE_PROJECT_LINK][/B][/COLOR][/I]/");
StorageReference photoRef = mStorageRef.child("Dir").child("File");

UploadTask uploadTask = photoRef.putStream(fileUri);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
System.out.println("DONE!");
//Get Download Address for Future
Uri dl = taskSnapshot.getMetadata().getDownloadUrl();
//Save Download Address in App Dir
SharedPreferences sharedpreferences = getSharedPreferences("Info", Context.MODE_PRIVATE);
SharedPreferences.Editor dlLink = sharedpreferences.edit();
dlLink.putString("path", String.valueOf(dl));
dlLink.apply();

infoFile.append("\n" + dl.getPath());
System.out.println("Download >> " + dl);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
System.out.println("!Damn!");
}
});
}

DOWNLOAD Function check out SourceCode

public void downloadFromUri(StorageReference storageRef) throws Exception {
//Create File
final File localFile = new File(Environment.getExternalStorageDirectory().getPath() + "/CloudAppTest_GeeksEmpire.jpg");

storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
System.out.println("DONE!");
//Using Downloaded File
Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath()); System.out.println("Downloaded >> " + localFile.getAbsolutePath());
imageFile.setImageBitmap(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
}

For Deleting & any Modification you Just Need to Declare StorageReference & Call Functions. check out SourceCode

Thanks for Supporting GeeksEmpire projects

​

Don't Forget To Hit Thanks
​