Showing posts with label image. Show all posts
Showing posts with label image. Show all posts

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.

Wednesday, February 15, 2017

Android Take Image From Camera or Gallery Using EasyImage Library

In development process that I have done, there are one project that had features "open image" from camera or gallery for displayed on application. For example like change the profile picture, upload some image, etc.

Today I going to show you how to took image from camera or gallery using EasyImage Library. This library pretty easy to use. You don't need to write much code (like manual way), because this library has function that specific open image from camera and open from gallery.

Let's start the steps one by one ..

Library 


In this project, I used some libraries. Please compile the following libraries in build.gradle (Module : app) :

compile 'com.github.jkwiecien:EasyImage:1.3.1'
compile 'com.github.bumptech.glide:glide:3.7.0'

On your second gradle, put the following code in build.gradle(Project:ProjectName)
 
allprojects {
    repositories {
        jcenter()
        maven { url 'https://jitpack.io' } //add this line
    }
}
 
 EasyImage is the library for open image from camera or gallery, and Glide is a library that used to load image.

AndroidManifest


In order to able to access and take image/photo from Gallery, then you need a permission on your AndroidManifest.xml :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Layout


The layout is not to complicated, I just uses one layout that contains button load image, imageview for display image and textview for display the path of the image. Give name as activity_main.xml and paste the following code :

The result look like the following Figure 1 :

Gambar 1
For the placeholder (the grey image above), you can find here.

File Java


I have created the method for open Camera and Gallery when button Open Image has clicked, with hope you easy to understand the steps. This java file named as MainActivity.java, paste the following code :

The result will look like the following giff :


That's all the tutorial about how to take image from gallery and camera using library EasyImage. Hope it useful.