【问题标题】:Http post high resolution image in androidHttp在android中发布高分辨率图像
【发布时间】:2012-12-08 09:12:41
【问题描述】:

我需要从相机中以最大分辨率(例如 12mpx)发布大图像。但是当我解码文件流以获取要发布的 byteArrayInputStream 时,我经常会遇到 OutOfMemoryError。有没有其他发大图的方法?

附:我不需要显示或缩放这张照片。

【问题讨论】:

    标签: android bitmap stream http-post


    【解决方案1】:

    是的,您可以通过 MultipartEntity 发布图像/文件,请在下面找到示例 sn-p:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    
    
    MultipartEntity entity = new MultipartEntity(
    HttpMultipartMode.BROWSER_COMPATIBLE);
    File file= new File(filePath);
    if(file.exists())
    {
        entity.addPart("data", new FileBody(file));
    }
    
    httppost.setEntity(entity);
    HttpResponse response = httpclient.execute(httppost);
    

    要使用多部分实体,您需要下载 httpmime-4.1.2.jar 并将其添加到项目的构建路径中。

    【讨论】:

      【解决方案2】:

      如果您使用的API 级别大于或等于11,请尝试在应用程序级别android:largeHeap="true" 的清单中使用此行

      【讨论】:

        【解决方案3】:

        如果可以以原始图像格式发布,则直接从文件流中发送数据:

        FileInputStream imageIputStream = new FileInputStream(image_file);
        
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        
        OutputStream out = connection.getOutputStream();
        
        copyStream(imageIputStream, out);
        
        out.close();
        imageIputStream.close();
        

        copyStream 函数:

        static int copyStream(InputStream src, OutputStream dst) throws IOException
        {
            int read = 0;
            int read_total = 0;
            byte[] buf = new byte[1024 * 2];
        
            while ((read = src.read(buf)) != -1)
            {
                read_total += read;
                dst.write(buf, 0, read);
            }
        
            return (read_total);
        }
        

        【讨论】:

          猜你喜欢
          • 2012-05-08
          • 1970-01-01
          • 2012-01-29
          • 1970-01-01
          • 2017-08-23
          • 2014-11-20
          • 2014-10-03
          • 2015-10-20
          • 2013-08-25
          相关资源
          最近更新 更多