I am basically tring to upload image from android and upload it to php server but here i'm not getting any connection with this code or image upload
.
I'm getting this error .
Error in http connection java.net.UnknownHostException: host name
but as per my knowledge i given correct connection and php file also in correct domain. Look at my code : UploadImage.java
public class UploadImage extends Activity {
InputStream inputStream;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://server.com/uploadimage/uploadimage.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
Toast.makeText(UploadImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
System.out.println("Error in http connection "+e.toString());
}
}
public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{
String res = "";
StringBuffer buffer = new StringBuffer();
inputStream = response.getEntity().getContent();
int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
Toast.makeText(UploadImage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();
if (contentLength < 0){
}
else{
byte[] data = new byte[512];
int len = 0;
try
{
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer…..
}
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
inputStream.close(); // closing the stream…..
}
catch (IOException e)
{
e.printStackTrace();
}
res = buffer.toString(); // converting stringbuffer to string…..
Toast.makeText(UploadImage.this, "Result : " + res, Toast.LENGTH_LONG).show();
//System.out.println("Response => " + EntityUtils.toString(response.getEntity()));
}
return res;
}
}
Php Code :
<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';?>
Any one known this issue ? if any one know how to store in mysql database from php file and fetch viceversa please suggest me here...
php" method="post" enctype="multipart/form-data"> Retrive all the images: <input type="submit" value="submit" name="retrive" /> </form> <? php //THIS IS INDEX. PHP PAGE //connect to database. db name is images mysql_connect("", "", "") OR DIE (mysql_error()); mysql_select_db ("") OR DIE ("Unable to select db".
Im trying to upload images into the database Make sure that FILE access is turned on - then in phpmyadmin you can use a FILE command in the insert that loads the image into the database.- for example INSERT INTO myimagetable (image_name, myimage) VALUES ('myimage.jpg', LOAD_FILE('images/myimage.jpg'));
Include the database configuration file to connect and select the MySQL database. Get the file extension using pathinfo () function in PHP and check whether the user selects only the image files. Upload images to the server using move_uploaded_file () function in PHP. Insert image file names in the database using PHP and MySQL.
The following steps need to be followed to upload an image and display it on a website using PHP: Create a form using HTML for uploading the image files. Connect with the database to insert the image file.
The dbConfig.php file is used to connect and select the MySQL database. Specify the database hostname ( $dbHost ), username ( $dbUsername ), password ( $dbPassword ), and name ( $dbName) as per your MySQL credentials. Create an HTML form to allow the user to choose a file they want to upload.
The problem is very clear ...
Error in http connection java.net.UnknownHostException: host name
means that the HttpPost
cannot make a connection using the hostname you supplied - because the hostname you supplied isn't known.
If you take the hostname from this line :
HttpPost httppost = new HttpPost("http://server.com/uploadimage/uploadimage.php");
and put it in a browser on the same device what happens ... i suggest you will get an error saying unable to connect to host. If this works then i suggest you check the following line is in your manifest :
<uses-permission android:name="android.permission.INTERNET" />
Also ensure that the PHP file contains the following header if your using a JPEG
:
header('Content-Type: image/jpg');
1. Need to add Internet permission in android manifest file .
<uses-permission android:name="android.permission.INTERNET" />
2. Every Time i used to see image using url but unable to see because i didnt added
echo "<img src=test.jpg>";
3.$file = fopen('test.jpg', 'wb');
4. final thing is i have to change header file as :
header('Content-Type: image/jpg; charset=utf-8');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With