Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading images to a PHP server from Android

I need to upload an image to a remote PHP server which expects the following parameters in HTTPPOST:

*$_POST['title']*
*$_POST['caption']*
*$_FILES['fileatt']*

Most of the internet searches suggested either :

Download the following classes and trying MultiPartEntity to send the request:

apache-mime4j-0.5.jar
httpclient-4.0-beta2.jar
httpcore-4.0-beta3.jar
httpmime-4.0-beta2.jar

OR

Use URLconnection and handle multipart data myself.

Btw, I am keen on using HttpClient class rather than java.net(or is it android.net) classes. Eventually, I downloaded the Multipart classes from the Android source code and used them in my project instead.

Though this can be done by any of the above mentioned methods, I'd like to make sure if these are the only ways to achieve the said objective. I skimmed through the documentation and found a FileEntity class but I could not get it to work.

What is the correct way to get this done in an Android application?

Thanks.

like image 649
Samuh Avatar asked Mar 30 '10 10:03

Samuh


People also ask

Can I upload images to phpmyadmin?

Check whether the user selects an image file to upload. Retrieve the content of image file by the tmp_name . Create the connection to MySQL database and select the database. Insert the binary content of the image in the images table.

Does PHP work on Android?

Install PHP on Android OS Step 1: Install the Termux app on your device. Step 2: After installation of the app, you will see a “$” sign. Type apt update and press the enter key on your keyboard. Step 3: After that, you will see the $ sign again.

How can I upload an image from a URL in PHP?

If url is proper then by using file_get_contents() function reads a image file into a string. And lastly by using file_get_contents() function, we have write image string to a file. So this way we have upload image from URL by using PHP with Ajax.


2 Answers

The Android source seems to come with an internal multipart helper library. See this post. At a quick glance, it's better documented than plenty of public APIs (cough cough, SoundPool, cough cough), so it should be a pretty good place to start, or possibly fine to just use a drop-in solution.

like image 151
Yoni Samlan Avatar answered Oct 01 '22 20:10

Yoni Samlan


Maybe this post on the official Android group helps. The guy is using mime4j.

Another helpful resource could be this example in the Pro Android book.

like image 30
znq Avatar answered Oct 01 '22 22:10

znq