Sending images and videos to server in android - android

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);

Related

How to upload Image and Video file using HttpPost in android

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

File Upload in Android

I am trying to upload a file along with some other text fields from android. I keep getting this error
org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
However if I do an upload without the file (i.e. only text fields) it is working.
The file upload works when I do it from a webpage and hit the same servlet.
This is how i'm working for uploading files this may be useful for you and if you have any questions while implementing you can cas me.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uploadUrl);
File file = new File(path);
if (file.exists()) {
FileBody fileBody = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", fileBody);
if (httpPost != null) {
httpPost.setEntity(reqEntity);
httpClient.execute(httpPost);

How to upload image using MultipartEntity in Android HttpPost?

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

Recorded video using android doesn't play on Blobstore

I wrote the code to record a video on android, I upload it to the Blobstore, but no luck in getting it play either from the player on googleappengine or stream it on a web application however, I can read the video when I download it. Any idea ?? I thought of the encodings I am using or the video format(.mp4) or even more, the way i send the bytes to the blobstore. thank you.
here is the recording code:
mCamera = Camera.open();
path= Environment.getExternalStorageDirectory().getAbsolutePath().toString();
Date date=new Date();
filename="/rec"+date.toString().replace(" ", "_").replace(":", "_")+".mp4";
//create empty file it must use
File file=new File(path,filename);
mrec = new MediaRecorder();
mCamera.lock();
mCamera.unlock();
mrec.setCamera(mCamera);
mrec.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mrec.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mrec.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mrec.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mrec.setVideoFrameRate(20);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
mrec.setOutputFile(path+filename);
mrec.prepare();
mrec.start();
and here is the uploading code:
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
//HttpPost httppost = new HttpPost(url);
HttpPost httppost = new HttpPost(url);
File file = new File("/sdcard/"+videoName);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "video/mp4");
mpEntity.addPart("videoFile", cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
the "url" parameter is the generated url from the blobstore in order to upload the media file, i guess blobstore users are familiar with it.
Try using OutputFormat.MPEG_4, VideoEncoder.H264 and AudioEncoder.AAC.

uploading image on web: getting html source code as response

I am trying to upload image on web server, but whenever it tries it is sending me html source code as response and image is not uploaded there. My code is:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitMap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
byte[] buffer = byteStream.toByteArray();
ByteArrayBody body = new ByteArrayBody(buffer,"profile_image");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("b#gmail.com.jpg", body);
post.setEntity(entity);
System.out.println("post entity length "+entity.getContentLength());
ResponseHandler handler = new BasicResponseHandler() ;
String response = client.execute(post,handler);
Thanks in advance!!!
Look at this Example http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/
And change YourUrl

Categories

Resources