Reputation: 343
I am developing an android application and I need to upload an Image to Following URL.
http://demo1.idevtechnolabs.com/RChatAPI/usrPhotos/
When I upload image to this URL the image should be stored in
http://demo1.idevtechnolabs.com/RChatAPI/usrPhotos/YourImage.jpg
and user can see it when he/she want from this url.
Please I have no idea what to do for this. please help me or send me any link for that.
Thanks in advance!
Upvotes: 1
Views: 3490
Reputation: 876
Try this (using volley to upload image)
I am only writing snippets of upload method from android side and php file to receive the file(in the form of string)
ANDROID/JAVA code
// getStringImage method will be called inside uploadImage
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
private void uploadImage(){
//Showing the progress dialog
final ProgressDialog loading = ProgressDialog.show(this, "Uploading...", "Please wait...", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
//Disimissing the progress dialog
loading.dismiss();
//Showing toast message of the response
Toast.makeText(MainActivity.this, "uploaded" , Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
//Showing toast
Toast.makeText(MainActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(bitmap);
//Getting Image Name
String name = editTextName.getText().toString().trim();
//Creating parameters
Map<String,String> params = new Hashtable<String, String>();
//Adding parameters
params.put(KEY_IMAGE, image);
params.put(KEY_NAME, "name");
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
PHP side img.php
<?php
$host="localhost"; //replace with database hostname
$username="root"; //replace with database username
$password=""; //replace with database password
$db_name="mydb"; //replace with database name
$con=mysql_connect($host,$username,$password);
$db=mysql_select_db($db_name);
$name = $_REQUEST['name']; //image name
$image = $_REQUEST['image']; //image in string format
$user=$_REQUEST['User_ID'];
$decodedImage = base64_decode($image);
$image_file=time().rand(1111,9999);
$name=$name.$image_file;
$base_path='/var/www/html/uploads/';
file_put_contents($base_path.$name.".jpg", $decodedImage);
mysql_query("INSERT into `image`(`img`,`User_ID`,`date`) values ('".$image_file.".jpg','$user',now() )");
echo mysql_insert_id();
?>
Its a late reply but I hope it helps someone else :) If you have any trouble integrating the above code reply me .
Upvotes: 3
Reputation: 384
Follow the given step:
1)You've to convert your image into bitmap,
2)Convert the bitmap into Base64 String,
3)Send this string to server.
To represent this image:
1) Convert this Base64 into bitmap and set this bitmap into imageview.
Follow the given link
http://www.codeproject.com/Questions/464629/Pass-byte-as-parameter-to-JSON-service-from-Androi
Upvotes: 3