Android post Base64 String to PHP
This is the code for image:
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
This is the HTTP code for sending the image to the server:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://servername.com/uploadimage.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
If you find any more difficulties ask me or a check:
- this similar tutorial for image uploading
- Base64 example
In PHP header file changes :
header('Content-Type: image/jpg; charset=utf-8');
$base=$_REQUEST['image'];
Uploading A Base64 encode Image to php server
Look into the differences. Sometimes you need to replace spaces with plusses.
$binary = base64_decode(str_replace(" ", "+", $_POST['image']));
Android Image Upload to php server base64 not working
I solved the issue, it was a peculiar one to be honest. The java code is fine here as expected, the problem was that i didn't knew this weird php behavior,
i was giving the upload path with absolute path, like this
$upload_path = "android_pool/whatsapp/images/$name.jpg"
but php flags this as wrong for some reason, php wants the relative images directory path respect to where the php file i am calling is.
i know makes no sense, cause absolute path is always better and why php doesn't work with that i have no clue.
so what works is
$upload_path = "images/$name.jpg"
just by changing this everything works.
I solved this by fiddling around and changing things just for the sake of changing.
So i felt obligated to answer this unintuitive problem for some poor soul who encounters it.
How to send base64 image to server - PHP
There are several points you should check:
1.Make sure the server has received the base64 string
$encoded_string = $_POST['encoded_string'];
Check the length of $encoded_string
, it should have the same length as the android client says.
- Make sure the decoding works
$decoded_string = base64_decode($encoded_string);
Check the length of $decoded_string
, it should NOT be zero or something odd.
- Make sure the file written works
$is_written = fwrite($file, $decoded_string);
$is_written
should be the length of the data that has been written to the file, if it is a false
, then something is wrong.
Can't send Base64 String from Android to php
Using this implementation of "encodeToBase64" solved it. As greenapps suggested, we shouldn't use the bitmap to get the bytes of an image file.
private String encodeToBase64(final String url){
try {
File file = new File(url);
byte[] bFile = new byte[(int) file.length()];
FileInputStream inputStream = new FileInputStream(url);
inputStream.read(bFile);
inputStream.close();
return Base64.encodeToString(bFile, Base64.NO_WRAP);
} catch (IOException e) {
Log.e(TAG, Log.getStackTraceString(e));
}
return null;
}
Related Topics
How to Serve Documents from Outside the Web Root Using PHP
How to Do Aes256 Decryption in PHP
Unsetting Array Values in a Foreach Loop
Laravel - Model Class Not Found
Getting Output and Exit Status from Shell_Exec()
How to Pass a PHP Variable to Vue Component Instance in Laravel Blade
Heredoc Interfering with Code Indentation
Capture Newline from a Textarea Input
Php: Split Multibyte String (Word) into Separate Characters
Composer Cannot Download Files
PHP Recursively Unset Array Keys If Match
File Attachment with PHPmailer
Is There Any Sort of "Pre Login" Event or Similar
PHP and Microsoft Access Database - Connection and Crud
How to Use String Concatenation to Define a Class Const in PHP