Thursday , October 21 2021

Firebase Storage – Uploading and Downloading PDF Files – Android Coding by DMTechnolab

The package The trap.Simplify.Firebasstorage;

Import Android.Clearly;

Import Android.Content.Intent;

Import the evening.package manager;

Import Android.The hate;

Import Android.The.the construction of;

Import Android.The.Bundle;

Import Android.the provider.Adjustment;

Import Android.Support.Annotation.No no;


Import Android.Support.v7.App.AppCompatActivity;

Import Android.Opinion.Opinion;

Import Android.Widget.Change in writing;

Import Android.Widget.progress bar;

Import Android.Widget.text view;

Import Android.Widget.Toast;

Import With.Google.Android.Village.Work.OnFailureListener;

Import With.Google.Android.Village.Work.OnSuccessListener;

Import With.Google.Fire.The database.Database reference;

Import With.Google.Fire.The database.FirebaseDatabase;

Import With.Google.Fire.Storage.FirebaseStorage;

Import With.Google.Fire.Storage.OnProgressListener;

Import With.Google.Fire.Storage.Storage;

Import With.Google.Fire.Storage.UploadTask;

public class Main activity Is spread AppCompatActivity come into force Opinion.OnClickListener {{

// This is the pic pdf code used in file chaser.

The last Fixed int PICK_PDF_CODE = 2342 is;

// these are ideas

text view textViewStatus;

Change in writing editTextFilename;

progress bar progress bar;

// Firebase objects for storage and database

Storage mStorageReference;

DatabaseReference mDatabaseReference;

@ Override

Protected Zero Go ahead()Bundle saveInstanceState) {{

Super.Go ahead()saveInstanceState);


// obtaining firebase objects

mStorageReference = FirebaseStorage.get Instance()).refer to());

mDatabaseReference = FirebaseDatabase.get Instance()).refer to()continuously.DATABASE_PATH_UPLOADS);

// get ideas

textViewStatus = ()text view) findViewById()R.Id.textViewStatus);

editTextFilename = ()Change in writing) findViewById()R.Id.editTextFileName);

progress bar = ()progress bar) findViewById()R.Id.progress bar);

// Connecting listeners to ideas




// this function will get PDF from storage

Private Zero getPDF()) {{

// We need permissions asked at runtime for more than lollipop versions

// So if permission is not available then user will go to screen to grant storage permission

If ()the construction of.the version.SDK_INT > = the construction of.VERSION_CODES.M &&,


! = package manager.permission granted) {{

Intent is intended = New Intent()Adjustment.ACTION_APPLICATION_DETAILS_SETTINGS,

to hate.Parse()“Package:” + getPackageName())));

Beginning()is intended);

The return;


// create an intention for the file selector

Intent is intended = New Intent());

is intended.Settype()“Application / pdf”);

is intended.Setation()Intent.ACTION_GET_CONTENT);

startActivityForResult()Intent.Crocker()is intended, “Select picture”), PICK_PDF_CODE);


@ Override

Protected Zero onActivityResult()int request code, int results, Intent The data) {{

Super.onActivityResult()request code, results, The data);

// when the user loses the file

If ()request code == PICK_PDF_CODE && results == RESULT_OK && The data ! = Invalid && The data.get data()) ! = Invalid) {{

// if a file is selected

If ()The data.get data()) ! = Invalid) {{

// file uploading

Upload documents()The data.get data()));


Toast.Makeup()this, “no file chosen”, Toast.LENGTH_SHORT).Display());




// this method is uploading file

// code is same as previous tutorial

// so we are not explaining it

Private Zero Upload documents()to hate The data) {{

progress bar.Instability()Opinion.View);

Storage sRef = mStorageReference.child()continuously.STORAGE_PATH_UPLOADS + system.currentTimeMillis()) + “.pdf”);

sRef.Putile()The data)

.addOnSuccessListener()New OnSuccessListener<UploadTask.Task snapshot>()) {{

@Suppress Warnings()“Visifestest”)

@ Override

public Zero Failed()UploadTask.Task snapshot taskSnapshot) {{

progress bar.Instability()Opinion.Gone);

textViewStatus.Settext()“File uploaded successfully”);

Pour Pour = New Pour()editTextFilename.Gettext()).Wire()), taskSnapshot.getDownloadUrl()).Wire()));

mDatabaseReference.child()mDatabaseReference.Push it()).Give the key())).To value()Pour);



.addOnFailureListener()New OnFailureListener()) {{

@ Override

public Zero Failure()@Nonal exception exception) {{

Toast.Makeup()getApplicationContext()), exception.getMessage()), Toast.LENGTH_LONG).Display());



.addOnProgressListener()New OnProgressListener<Uploadtask.Task snapshot>()) {{

@Suppress Warnings()“Visifestest”)

@ Override

public Zero In progress()Uploadtask.Task snapshot taskSnapshot) {{

Double Progress = ()100.0 * taskSnapshot.getBytesTransferred())) / taskSnapshot.getTotalByteCount());

textViewStatus.Settext()()int) Progress + “% Uploading …”);




@ Override

public Zero on click()Opinion Opinion) {{

to switch ()Opinion.GetId())) {{

matter R.Id.buttonUploadFile:



matter R.Id.textViewUploads:

Beginning()New Intent()this, ViewUploadsActivity.class));





Source link

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

One comment

  1. NYC NY Commercial moving companies

    Excellent website. Lots of useful data in this article. I’m sending it to some buddies ans Also sharing in delightful. And of course, many thanks in the work!

Leave a Reply

Your email address will not be published. Required fields are marked *