Friday 30 December 2022

Android Upload Image using Android Upload Service

 

Creating Server Side Codes for Android Upload Image

The first thing we need is to create our server side web services. For server side I am using PHP and MySQL. And for this I am using Wamp server. You can still use xampp or any other application. Now follow the steps to create your web service to handle the file upload.

  • Go to localhost/phpmyadmin and create the following database table.

android upload image database.jpg

  • Now inside your server’s root directory (c:/wamp/www) and create a new folder. I created AndroidUploadImage.
  • Inside the folder create a folder named uploads, in this folder we will save all the uploaded images.
  • Create a file named dbDetails.php and write the following code.

  • Now create a file named upload.php and write the following code.

  • Now test your script. You can use a rest client to test it I am using POSTMAN. See the below screenshot.

android upload image web service

  • If you are seeing the above response then. Your script is working fine. You can check the database and upload folder which you have created.
android upload image database
Database
android upload image database
Upload Directory
  • So its working absolutely fine. Now lets move ahead and create a android project.

Creating Android Studio Project

We are done with the server side coding for this Android Upload Image Example. Now lets code our android application. Follow the steps given.

  • Create a Android Studio Project.
  • Create a class named Constants.java and write the following code. The following class contains the path to our php file which we created.  You are seeing two strings. The second it the path to the file we will create at the end of this post.

  • You need to change the IP according to your system. To know the IP you can use IPCONFIG command in command prompt (windows user).
  • Now we need to add android upload service to our project.

Adding Android Upload Service

  • Go to your app level build.gradle file and add the following line inside dependencies block and sync your project.

  • Come to activity_main.xml and write the following xml code.

  • The above code will generate the following layout.

android upload image example

  • As you can see we have two buttons, one to select image and other to upload image. We also have an EditText to enter the name for the image.
  • Now come to MainActivity.java and write the following code.

  • Finally add the storage and internet permission on your manifest.

  • Thats it now just run your app.

android upload image example app

  • Bingo! Its working absolutely fine. Now you may need to fetch the uploaded images.

Fetching the Uploaded Images

  • To fetch the images inside your server side project, create one more file named getImages.php and write the following code.

  • This code will give you the following JSON.

  • Now you can use the following tutorial to display the images using the above JSON.

No comments:

Post a Comment