在android中将图像和视频发送到服务器

时间:2013-10-10 09:08:39

标签: android json image web-services video

我正在创建一个用于拍摄照片和视频的Android应用程序。捕获图像后,我想将此图像与日期和一些文本发送到Web服务器。在服务器端,我正在使用这些图片和视频制作应用程序。拍摄的图像将保存在存储卡中。如何使用JSON发送带有文本的图像。我也想将视频发送到网络服务器。

3 个答案:

答案 0 :(得分:1)

您可以使用Multipart post请求执行此操作:(这样,您不需要创建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);

您必须添加此mime4j库。

答案 1 :(得分:1)

尝试在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);

答案 2 :(得分:0)

您可以在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);