Simplified Coding Android Upload Image to Python Server

Hullo everyone, "Uploading File or Epitome to Backend Server" this is something that we often need to do in many applications. Nosotros already learned this concept in many previous posts. But things keep changing in this tech world and erstwhile methods are obsolete now. That is why hither I am with this Android Upload File to Server with Progress Tutorial.

The method nosotros will learn in this post is fully updated and volition work in all devices, including Android x.

Contents

  • 1 Prerequisites
  • two Android Upload File to Server with Progress – Video
  • 3 Designing Database
  • 4 Building Epitome Upload API
  • 5 Android Upload File to Server with Progress
    • 5.one Creating an Android Project
    • 5.2 Calculation Dependencies
    • 5.3 Designing User Interface
    • 5.four Choosing an Paradigm from Androids Gallery
    • v.5 Setting Up Retrofit for File Upload
    • 5.half-dozen Creating Some Extension Functions
    • v.vii Creating Custom RequestBody
  • 6 Android Upload File to Server with Progress Source Lawmaking

Prerequisites

  • XAMPP Server: We volition use it for the backend, and ofcourse we volition be using PHP and MySQL hither for the backend part. Simply if you lot are comfy with some other tech for handling the backend you tin exercise it. We basically merely demand the API that will accept a file.
  • Retrofit: Nosotros will be using this networking library in our Android Projection.

If you want to learn building RESTFul API, that you can utilize in your production awarding and so please subscribe to my YouTube aqueduct and comment to any video that you want a product level RESTFul API Evolution Tutorial.

Subscribe to Simplified Coding

You tin can also learn the basics of RESTFul API Evolution using SLIM3 PHP Framework from the below given link.

Android Networking Tutorial using PHP, MySQL & Retrofit

At present let's motion into our post. We will start by designing our database.

What we will do is nosotros will store the uploaded image to an uploads directory in the backend server and we will shop the path to that prototype in our MySQL database. And you should remember this thing, nosotros don't store files in our MySQL database, nosotros just store path to the file in the database.

Android Upload File to Server with Progress – Video

In case you lot are more than comfortable watching video tutorial, and so yous tin can watch the to a higher place playlist that explains complete step by step guide about Uploading File to Server with Progress.

But in case you are ok reading a written tutorial, keep reading, nosotros volition practise the same thing here.

Designing Database

  • Nosotros volition be using a very simple table. So simply open up PhpMyAdmin (I hope your XAMPP Server is running) and create a database. In my case I accept created a table named ImageUploader.
  • Now we need to create a tabular array where we will shop the uploaded files info. Run the post-obit SQL query to create a tabular array named uploads.
  • As you can see we have 3 columns in our tabular array (id, path and description).

Building Image Upload API

  • We take the database, now let's build the Image Upload API. For this I have a directory named ImageUploader inside my root folder that is htdocs for xampp.
  • Inside this directory we volition create a file named DbConnect.php.
  • The in a higher place file will make our database connection. Now let'due south create the script that will handle the file upload.
  • Create a file named Api.php and write the post-obit code.
  • The above code handle two cases. The commencement 1 is to upload the file and the next is to fetch the uploaded files.

If you lot need the source lawmaking of the PHP Scripts, and then yous can get it from the below given link.

Download PHP Scripts

So we take the APIs set up, now permit'due south create our Android Project.

Android Upload File to Server with Progress

Creating an Android Project

  • I take created a new project named ImageUploader using an EmptyActivity. Once the project is loaded, we will add required dependencies and internet permission.
  • First add together internet permission in your AndroidManifest.xml, you also need to add the usesCleartextTraffic="truthful".
  •   At present we volition add the required dependencies.

Adding Dependencies

  • Open app level build.gradle file and add the post-obit dependencies inside dependencies cake.
  • After calculation these lines, sync the projection.

Designing User Interface

  • At present let's design the UI. Open activity_main.xml and write the following xml code.
  • The above XML Code volition generate the following output.
android upload file to server
Android Upload File to Server with Progress
  • Every bit yous tin encounter we have a very simple User Interface for our upload performance.
  • At present we need an Image to Upload it. And we will let the user choose the image from device. So our first chore here is to let the user cull the prototype to upload.

Choosing an Image from Androids Gallery

  • First define a var inside your MainActivity.kt.  This var volition keep the uri of the selected image file.
  • Now nosotros will create two different function inside our MainActivity.kt. One is to choose the paradigm that we volition employ now and other to upload the image.
  • We will also attach the click listeners to our ImageView and Button to phone call the functions equally you can meet in the below code.
  • The above code snippet you need to put inside your onCreate() part.
  • Within MainActivity.kt we will define a constant that we will use for getting the selected paradigm.
  • Now we will define the role openImageChooser() that will open the prototype picker, and we will also override the role onActivityResult() to get the selected prototype.
  • As y'all tin encounter within the function onActivityResult() we are storing the uri of the image to the var that nosotros defined at the beginning. We are too displaying the image selected in our ImageView.

Here we accept the tricky office, we have the Uri of the epitome, simply to upload the image we need the real path of it. If you accept seen my final tutorial about Uploading Image with Retrofit, then here we have used a function that provides the real path from Uri. Merely this doesn't work anymore in Android x.

Nowadays, android recommends us to use the app directory for file storage. Every app has its own app-specific storage directory for file storage that is not accessible to other apps, and all files stored here are removed when we uninstall the application. And the expert matter is too admission this expanse you lot don't demand any permission.

So what nosotros need to do is, we need to select the file from the gallery and make a re-create of it inside the app-specific storage directory.

But earlier doing the above mentioned thing we will setup the Retrofit.

Setting Up Retrofit for File Upload

If you lot have used Retrofit earlier, yous know what we need to do. First we volition create an interface to ascertain the API Calls.

  • Create a file named MyAPI.kt and write the following code in it.
  • In the to a higher place code y'all can see we didn't utilize localhost in the Base URL simply we used IP Address, now what is the IP Address in your system yous need to find it out. Cheque this post to know the details about it.
  • Now yous too need to create the following data course for parsing the API Response.

Creating Some Extension Functions

Extension function is a bang-up feature that you go with Kotlin. Nosotros need to create two extension functions in this project, one is for getting file name from Uri and another one is for displaying snackbar (Toast looks very ho-hum at present a days).

  • Create a file named Utils.kt and write the following code.
  • Now let'south make the very important office of this project, that is a custom RequestBody.

If we desire to go the upload progress, nosotros demand to create a custom RequestBody that volition comprise the file and the other information that we demand to send with the API.

Creating Custom RequestBody

  • Create a course named UploadRequestBody and write the following lawmaking in it.
  • Now we just need to complete the function uploadImage() within our MainActivity.kt and nosotros are washed.
  • After finishing the above role, you can try running your application to run into the image is getting uploaded or not.

Android Upload File to Server with Progress Source Code

In case y'all want to get my source lawmaking, y'all can become it by being Simplified Coding's Subscriber on YouTube.

So that is all for this Android Upload File to Server with Progress Tutorial friends. In example you accept any trouble or confusion, you lot can get out your comments below. And if you found this mail helpful and so please help me back past SHARING it with your friends. Thank You 🙂

wilsoninen1996.blogspot.com

Source: https://www.simplifiedcoding.net/android-upload-file-to-server/

0 Response to "Simplified Coding Android Upload Image to Python Server"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel