Wednesday, November 8, 2017

Insert Image From Android To Database (PHP + MySQL)



This article will show a simple tutorial about how to insert image from android to database. Insert image from android to database possible achieve by creating local database using PHP and MySQL.

So this tutorial using two projects, 1 PHP project and the other one is Android project. Don't forget to create your database to store the image that received from Android.

Image will be send from android by send its json to server. In php project, we also create a file that handle json from mobile (android).

Scenario 


We will save the file of image in the directory inside the php project. In database we just store the path of the image from php project.

For example
  • Users choose image obama.jpg from their android
  • In PHP project, there is a directory named imagefromandroid.
  • In database, just store its path, so it's become : imagefromandroid/obama.jpg.

Create Database


In this project i just create simple database, only one table. Please adjust your project and your database.

Database

PHP Project


Create a directory that used to save the file of image that received from android. Take a look at the following image

Directory

I above figure, i create a directory where image from mobile android will stored name imagefromandroid. Also, we will use two php classes, Connection.php and insertImageFromMobile.php.

Connection.php


This file used to connect your PHP Project to your database. The code look like the follows :

InsertImageFromMobile.java


This class used to handle data that received from mobile in json form. The code is look like the follows :


Android Project


There are several classes and xml files that we create in order to able to send image to the server or database.

LoggingInterceptors.java


This class created for handle response of retrofit. In other word, we able to see request process of retrofit to the endpoint.


ResultModel.java


This class will handle response message of request. The response message that will shown by this class are success or failed.



ApiService.java


This class is an interface. In this class will declare endpoint of BASE URL.


ApiClient.java



This class contains the BASE URL that used in this project. And also in this class contains initialized of the retrofit

MainActivity.java


All class or method that used to send image to the server will called in this class. Full code of MainActivity :


Okay, that's all. Hope it helps you to learn how to insert image from android to database.

Related Posts

1 komentar so far

hi sir, where did you put LoggingInterceptor?


EmoticonEmoticon