I'm trying to upload an image using the following code:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://konsole-data.de/uploadtest/upload.php");
MultipartEntity multiPart = new MultipartEntity();
multiPart.addPart("picture", new FileBody(new File(path)));
httpPost.setEntity(multiPart);
try {
HttpResponse res = httpClient.execute(httpPost);
Toast.makeText(getApplicationContext(),res.toString(),
Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
path is a String which identifies the image like /mnt/sdcard/DCIM/12712.jpg The connection works BUT no image is uploaded to the server, you can see a debug file here: http://konsole-data.de/uploadtest/data/20121214-144802-.dbg
What am doing wrong?
You should probably specify the HttpMultipartMode, and the MIME type of the file (but this is not necessary i think):
MultipartEntity multipart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
FileBody bin = new FileBody(new File(path), "image/jpeg");
multipart.addPart("picture", bin);
EDIT:
You should also check if you use the right path. Instead of creating the File object as an anonymous inner class:
File file = new File(path);
if(file.exists()){
FileBody bin = new FileBody(file, "image/jpeg");
multipart.addPart("picture", bin);
} else {
Log.w(YourClass.class.getSimpleName(), "File " + path + " doesn't exist!");
}
Related
how to upload bin file from internal storage to http server in android?
I'm getting permission denied error.
String url = "url_path";
String PATH = "/storage/emulated/0/data/";
Log.v("response path ", "PATH: " + PATH);
File file = new File(PATH,
"/test.bin");
if(file.exists()){
Log.d("response : "," file available");
} else {
Log.d("response : "," file not available");
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
Error Log:
java.io.FileNotFoundException: /storage/emulated/0/data/test.bin (Permission denied)
You have permissions for your App's data directory only. unless you have rooted android device.
or
A System app.
I need to upload Image and Video File with multiple parameters like File Name, Description , Height and Width using HttpPost method.
Thanks,
Suggestion appreciated.
For uploading a file the efficient way is using HttpPost with multipart/form
Multipart/form The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storages will be cleared at the end of request processing
Refer this Upload Files from Android to a Website/Http Server using Post
try this code
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
//Path of the file to be uploaded
String filepath = params[0];
File file = new File(filepath);
ContentBody cbFile = new FileBody(file, SET_MIMETYPE);//"audio/basic"
try {
mpEntity.addPart(FILE_NAME, cbFile);
post.setEntity(mpEntity);
HttpResponse response1 = client.execute(post);
HttpEntity resEntity = response1.getEntity();
} catch (Exception e) {
e.printStackTrace();
}
or also refer this link
"http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/"
Thanks
I am creating an android application for taking photos and videos. After capture images I want to send this image with date and some text to web server. In server side I am making an application with this pictures and videos. The image captured will be saved in memory card. How can I send image with text using JSON. Also I want to send Videos to the web server.
You can do this with a Multipart post request:(This way, you dont need to create json)
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serverURL);
MultipartEntity postEntity = new MultipartEntity();
File file = new File("Your File path on SD card");
postEntity.addPart("fileupload", new FileBody(file, "image/jpeg"));
postEntity.addPart("loginKey", new StringBody(""+loginKey));
postEntity.addPart("message", new StringBody(message));
postEntity.addPart("token", new StringBody(token));
post.setEntity(postEntity);
response = client.execute(post);
You have to add this mime4j library.
try this to uplaod text ,image to server in asynctask
FileBody filebodyVideo = new FileBody(new File(
"Sd Card VideoPath"));
FileBody filebodyVideo1 = new FileBody(new File("Video Upload url"));
StringBody Title= new StringBody("Put Title Here");
StringBody description= new StringBody("Put Desc Here");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("image", filebodyVideo);
multipartContent.addPart("Title", Title);
multipartContent.addPart("Description", description);
httppost.setEntity(multipartContent);
You can use this code in your asynctask:
File file = new File("Your File path on SD card");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("YourUrl");
MultipartEntity entity = new MultipartEntity();
entity.addPart("YourKey",new StringBody("Your Text"));
entity.addPart("File", new FileBody(file));
httpost.setEntity(entity);
HttpResponse response = httpclient.execute(httpost);
Image not upload using MultipartEntity.
Gives status code 200 but image not updated on serverside.
String responseBody;
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(
"http__zz/upload_picture?key=abc&property_id=10");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).toString()
+ "/Camera/Test.jpg");
ContentBody encFile = new FileBody(file, "image/png");
entity.addPart("picture", encFile);
request.setEntity(entity);
ResponseHandler<String> responsehandler = new BasicResponseHandler();
responseBody = client.execute(request, responsehandler);
if (responseBody != null && responseBody.length() > 0) {
Log.w("TAG", "Response image upload" + responseBody);
}
Try using a ByteArrayBody instead of a FileBody:
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).toString()
+ "/Camera/Test.jpg");
Bitmap b = BitmapFactory.decodeFile(file;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
b.compress(CompressFormat.JPEG, 100, bao);
ByteArrayBody body = new ByteArrayBody(bao.toByteArray(), "image/jpeg", "picture");
entity.addPart("picture", body);
Why don't you try to send it as base64 encoded string?
the best way to do this is to implement an IntentService and notify status with broadcast intents. please check out this code from git its work for me
https://github.com/alexbbb/android-upload-service
I am adding a photo after checkin. I am using that checkinId to add photo. I am getting error response from post request.
{"meta":{"code":400,"errorType":"param_error","errorDetail":"Cannot add photo to this checkin (it is a duplicate)"},"response":{}}
String URL_UPLOAD_PHOTO = "https://api.foursquare.com/v2/photos/add";
entity.addPart("v", new StringBody(sdf.format(cal.getTime())));
entity.addPart("checkinId", new StringBody(checkinId));
entity.addPart("public", new StringBody("1"));
entity.addPart("oauth_token", new StringBody(FoursquareConstants.sharedPreference.getToken()));
ByteArrayBody imgBody = new ByteArrayBody(FrameActivity.tempPicByte, "image/jpeg", "happyPhoto");
entity.addPart("image", imgBody);
What's the problem?
This means the photo has already been uploaded.
The below code works for me when uploading images.
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://api.foursquare.com/v2/photos/add");
try
{
MultipartEntity entity = new MultipartEntity();
entity.addPart("v", new StringBody("20121210"));
entity.addPart("venueId", new StringBody(venue.getId()));
entity.addPart("public", new StringBody("1"));
entity.addPart("oauth_token", new StringBody(mAccessToken));
ByteArrayBody imgBody = new ByteArrayBody(bitmapdata, "image/jpeg", "FS_image");
entity.addPart("image",imgBody);
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
Log.v("response","" +response);
responseResult = inputStreamToString(response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e)
{
Log.d(TAG, "Opening URL " +e);
}