Showing posts with label store. Show all posts
Showing posts with label store. Show all posts

Sunday, April 29, 2018

Android Tutorial How To Insert Image Into Firebase Storage

Lately, Google has recommend to use Firebase as your storage when you build an application (web or mobile application). Firebase also known as serverless. Serverless means you no more need API for transfer/send data, with firebase it will be done.

Firebase has much features that can use in you application, there are :
  • Firebase Realtime message
  • Firebase Cloud Firestore
  • Firebase Cloud Function
  • Firebase Storage.
You can read all of these features in firebase official website to get more information about those features.

In this article I will show you how to insert image into Firebase Storage. 


Create your application On Firebase


1. Go to Firebase Console and create your project


2. Choose your platform (Choose Android)


3. Add your SHA-1 Key and your package name


4. Download the google-services.json


5. And place the google-services.json inside you app folder



Gradle Setting


Now let setup your gradle and add some dependencies of Firebase

build.gradle (Project:YourProjectName)

build.gradle (Module:App)

Ok your gradle has been setup, now turn to create the process how to insert image into Firebase Storage.


Java And XML Code


activity_main.xml

MainActivity.java

Chose your image from gallery or taken from camera using Edmodo Image Cropper


After image taken, process the image inside method OnActivityResult and upload it into Firebase Storage


Take a look at this code :


Here is the result of the above code :


That code save your image into path photo_profile/my_photo.jpg (last child will be set as the name of the image). By using this path (same path), the new image that you store will replace the old image, why? Because you put the file into the same path like before.

So, how to store much image into Firebase Storage? Easy. Just replace the last child using Current Milliseconds


The result of the above code will be like this :


After your image has stored into Firebase Storage, now use the following code to display the image into your ImageView

Full Code of MainActivity :

Run your application, select image and click button upload, then the image will stored into Firebase Storage, Goodluck!

Friday, December 30, 2016

Android Populating Spinner Data From SQLite Database

In previous tutorial i also created CRUD (Create, Read, Update, Delete) function using database SQLite. On that tutorial there is a condition where the data must be displayed on spinner.

If you are newbie in Android, it my previous tutorial maybe too complicated for you to get which point is actually the spinner function.

To make you easy understand the process, so i creating this tutorial, especially how to populating spinner data from SQLite.

If you need to go through to the CRUD tutorial, you can read here : Android SQLite Database Tutorial

Let's start created this tutorial. As always a provide you a video demo in order you get what the output of this tutorial.

VIDEO DEMO



IMPLEMENTATION

First, create model of your data, in this case i name my model as OwnerModel.java, then paste the following code :

Second, create java class that called DatabaseHandler.java. Paste the following code :

Attention :
In code above we define all the main function of CRUD, the query, or anything that related with database. I Have explained this class on previous tutorial here : Android SQLite Database Tutorial

Third, create MainActivity's layout. Usually, the name is always activity_main.xml. After that paste the following xml code :

Fourth, In your MainActivity.java will do much technical process like, input the data, display data to spinner and clear/delete all data from SQLite database. Paste the following code :

Run your project, hope it works well. Thank you.

Thursday, December 29, 2016

Android Store And Retrieve List To/From SharedPreference

In development, we sometimes need a temporary storage to store our data. Temporary storage used to passing our value from one activity to other activity.

There are a lot of ways that we can use, there are :
  1. Using sharedPreference
  2. Using Intent, or
  3. Using SQLite database
For SQLite database your can read here : Android SQLite Database Tutorial

If we need to passing a single value to other activity, we pretty quiet use Intent or sharedPreference. Both of those have a method to store a single value
  • SharedPreference : putString("","")
  • Intent : putExtra("","")
But in other condition we have to passing list to other activity, how to do it? 


Well, this tutorial going to show you how to store a List or ArrayList in SharedPrefences. But we have compile Gson in our build.gradle(Module:app)

compile 'com.google.code.gson:gson:2.6.2'

Gson used to convert list or arraylist as string (toJson()) before store to the shared preferences. Here the method to store list on sharedPrefences :


And when we try to retrieve the data, we have to convert the value that we get from sharedPreference become an arrayList, using Gson (fromJson()). Here the method to retrieve list from sharedPreference :

Your can get the value as list by using like the following code :

usage : String [] listItem = getListAsAString(Constants.KEY_SHARED);
Hope my description above help you. Thank you.

Android SQLite Database Tutorial (Create, Read, Update, Delete)

Sometime we need a database to support our development. In mobile, especially Android we using database SQLite.

SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.

In this tutorial i going to show you how to do CRUD (Create, Read, Update, Delete) on Android using Database SQLite.


VIDEO DEMO APLICATION


There are 3 buttons action (Insert, Update and Delete) and 1 Listview to display the saved data. You can see as realtime.

PREPARATION

Create some xml files that we in this project :
  1. activity_main.xml
  2. popup_delete.xml
  3. popup_update.xml
Create some java classes :
  1. DatabaseHandler.java
  2. ProfileModel.java
  3. MainActivity.java
In this project i store all the string in strings.xml. Here it is :

<resources>
    <string name="app_name">SQLLiteTutorial</string>
    <string name="input_your_name_bastard">Input your name, bastard</string>
    <string name="your_phone_number">your phone number</string>
    <string name="msg_cannot_allow_empty_field">Cannot allow empty field</string>
    <string name="lbl_update_data_pop_up">Update Data!</string>
    <string name="lbl_choose_an_id">Choose an ID</string>
    <string name="hint_update_phone_number">Update phone number</string>
    <string name="hint_update_name">Update name</string>
    <string name="lbl_save">Save</string>
    <string name="lbl_ok">OK</string>
</resources>

1. Before we create the process in Java files, first modify all the xml file that we prepared before

Open activity_main.xml, paste the following code :

Open popup_delete.xml. This layout appears when button delete is pressed. Paste the following code :

Open popup_update.xml. This layout appears when button update is pressed. Paste the following code :

2. Next we modify all the java classes.

First create the model of your object. In this project i created as ProfileModel.java. Paste the following code :

Take a look at method public String toString(). This method actually created to make us easy to display data on the spinner. You can modify the return value of this method related with your requirements.

Open DatabaseHandler.java. This class will extends SQLiteOpenHelper, implement its methods. The methods that we implement after extends SQLiteOpenHelper are :
  1. (override)  onCreate(SQLiteDatabase sqLiteDatabase). This method  used to create the table of database
  2. (override)  onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1). This method used to increase database version after any changes in its attribute of name automatically. Beside that this method also check if there any duplicated name of table or not.
Beside create database name, also in this class we create all the main function of CRUD :

Create the data or Insert :

Read all the data that we have inserted. This method will return your list model (In this case : return profileList) :

Update the data that we have inserted using parameter ID (ID of the profile) :

Delete the data that we had using parameter ID (ID Profile) :
Those methods will do CRUD action in our database. Here the full code of DatabaseHandler.java :
After that, the process moves to MainActivity.java. In this class we do the technical process of CRUD. And also in every technical process will calling the methods from DatabaseHandler.java.

When button insert is pressed, program will call method insertData(handler) :

After inserted and success, data will displayed on listview as realtime using method displayAllData(DatabaseHandler handler, ProfileModel profile) :

When button update is pressed, program will call method updateDataFromPopup() :

When the button update is pressed, there is a popup will appears. You have to choose ID that you will update, after that, fill or rewrite the value of the editText that you want to change.

Same as button update, when button delete pressed, there is a popup will appears. You just choose an ID that you want to delete. Here is the method popupDelete() :

Hope you understood how the process. And here is the full code of MainActivity.java :

Done! Run your project and, wish it works well. Thank you.