Upload image on Firebase storage

Upload image on Firebase storage Uploading an image to any server is quite a hectic task and it also as for so much of memory and processing time on the front end. This can be achieve with new RxJava By using this we can create multiple task with quite a minimum time.

What is Firebase Storage?

Cloud Storage for Firebase lets you upload and share user generated content, such as images and video, which allows you to build rich media content into your apps. Your data is stored in a Google Cloud Storage bucket, an exabyte scale object storage solution with high availability and global redundancy. Cloud Storage lets you securely upload these files directly from mobile devices and web browsers, handling spotty networks with ease.

Why RxJava?

RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.

How to upload image on firebase

How to Upload image on Firebase storage

1- First you need to add the android app to Firebase Console if not done yet you can get through this from here.

2- Add firebase storage to your firebase console.

3- In this tutorial we are using dexter for permission and glide.

4- Add Some helper class For camera upload CameraIntent.java and helper.java.

5- Add layout file activity_sign_up.xml.
<?xml version="1.0" encoding="utf-8"?>




                app:layout_collapseMode="pin" />


6- Now signup activity.

Create a function for upload image

private UploadTask saveImage(Bitmap image_bitmap_load, StorageReference storage, String file_name_small,int type) {

    //Play with bitmap
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image_bitmap_load.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    final byte[] data = baos.toByteArray();
    Date date = new Date();

    final StorageReference ref = storage.child(file_name_small);
    UploadTask  uploadTask = ref.putBytes(data);

    Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
            if (!task.isSuccessful()) {
                Toast.makeText(SignUpActivity.this, "unable to upload", Toast.LENGTH_SHORT).show();

                throw task.getException();

            return ref.getDownloadUrl();
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                                 public void onComplete(@NonNull Task<Uri> task) {
                                     if (task.isSuccessful()) {
                                         Uri downloadUri = task.getResult();

                                     } else {
                                         // Handle failures
                                         Toast.makeText(SignUpActivity.this, "unable to upload", Toast.LENGTH_SHORT).show();

    return uploadTask;

7- And initialize observable
private final CompositeDisposable disposables = new CompositeDisposable();

Observable<UploadTask> sampleObservable(final UploadTask large_image_task, final UploadTask normal_image_task, final UploadTask small_image_task) {
    StorageReference storage = FirebaseStorage.getInstance().getReference();
    return Observable.defer(new Callable<Observable<UploadTask>>() {
        @Override public Observable<UploadTask> call() throws Exception {
            // Do some long running operation
            //   SystemClock.sleep(5000);

            return Observable.just(large_image_task,normal_image_task,small_image_task);

8- Add task to disposables

// Run on a background thread
// Be notified on the main thread

.subscribeWith(new DisposableObserver<Task>() {
@Override public void onComplete() {
Log.d(TAG, "onComplete()");


@Override public void onError(Throwable e) {
Log.e(TAG, "onError()", e);

@Override public void onNext(Task task) {
Log.d(TAG, "onNext(" + task + ")");

It will trigger oncomplete File will be uploaded to the firebase storage. You can comment below for any queries. You can also get the code from here.

That’s all for how to upload image using Rx java.

Tagged : /

Shuffle deck of cards java

For Shuffle a deck of cards dynamically using java simply just fill array with random numbers between 0 to 51 and then remove one by one when the card is drawn we have here suit between 0 to 3 and rank between 0 to 13.

Now, To create a function to Shuffle a deck of cards dynamically using java.

Create an array of suit from 0 – 3 and rank from 0 – 12. Then add it to an array of deck of length 52 and shuffle the deck array randomly.

public static int[] cardShuffle() {

    // create a deck of 52 cards
    int[] suit = {0, 1, 2, 3};
    int[] rank = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12
    int SUITS = suit.length;
    int RANKS = rank.length;

    int N = 52;
    int M = 12;

    int[] deck = new int[N];
    // initialize deck
    for (int j = 0; j < SUITS; j++)
        for (int i = 0; i < RANKS; i++)
            deck[SUITS * i + j] = rank[i] + suit[j] * 13;

    // create random M cards
    for (int i = 0; i < M; i++) {
        int r = i + (int) (Math.random() * (N - i));
        //String t = deck[r];
        int t = deck[r];
        deck[r] = deck[i];
        deck[i] = t;
    return deck;

Now function for drawn N cards from the above deck of cards. Suppose you have 3 user each one should contain 4 cards so pass the numberOfCardsDrawn to 3×4 = 12. Then easily you can distribute each one 4 cards.

public static int[] drawCards(int[] deckOfCards,int numberOfCardsDrawn) {
    int[] drawnCards = new int[numberOfCardsDrawn];
    for (int l = 0; l < numberOfCardsDrawn; l++)
        drawnCards[l] = deckOfCards[l];
    return drawnCards;


Now just call both the function for the desired result.

    public static void main(String []args){
    int numberOfCardsDrawn = 12;

    int[] deckOfCards =  cardShuffle(); // shuffle cards
    int[] drawCardsFromDeck = drawCards(deckOfCards, numberOfCardsDrawn); // draw cards

    for (int i = 0; i <drawCardsFromDeck.length ; i++) {
        System.out.print(drawCardsFromDeck[i] + ", ");


Then finally take the cards and print by using system out Sample output of the above code snippet

Output: 19, 4, 49, 14, 47, 1, 39, 18, 48, 40, 41, 12

Github link for the code ShuffleCards.java .

Note : Output will be different each time because of the random function used in the program. You can also see other post from here.

Tagged : / / / / /