【问题标题】:Sending images and videos to server in android在android中将图像和视频发送到服务器
【发布时间】:2013-10-10 09:08:38
【问题描述】:

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

【问题讨论】:

    标签: android json image web-services video


    【解决方案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 库。

    【讨论】:

    • 所有类型的文件都可以吗
    • 目前我只尝试过视频和图片。但我确信任何文件都可以工作。
    【解决方案2】:

    尝试在异步任务中将文本、图像上传到服务器

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

    【讨论】:

      【解决方案3】:

      您可以在异步任务中使用此代码:

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

      【讨论】:

      • 这也是可能的库httpp://www.java2s.com/Code/Jar/h/Downloadhttpmime43jar.htm
      猜你喜欢
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多