首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何修复我的android照片上传,以发送更大的数据流

问我如何修复我的android照片上传,以发送更大的数据流
EN

Stack Overflow用户
提问于 2010-10-20 19:59:42
回答 2查看 1K关注 0票数 1

我有一个android应用程序,试图通过API接口将照片上传到web服务器。只要图像大小不是太大(在1280x800左右失败),该方法就能正常工作。这里的代码可以工作在较小的图像上,但在较大的图像上就会失败。有什么建议吗?

代码语言:javascript
复制
public void uploadPhoto(FileInputStream fileInputStream, String session_key) throws IOException, ClientProtocolException {

    URL connectURL = new URL(API_ENDPOINT_URL + "session_key="
            + session_key + "&method=" + ApiMethods.UPLOAD_PHOTO);
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();

    // Allow Inputs
    conn.setDoInput(true);

    // Allow Outputs
    conn.setDoOutput(true);

    // Don't use a cached copy.
    conn.setUseCaches(false);

    // Use a post method.
    conn.setRequestMethod("POST");

    conn.setRequestProperty("Connection", "Keep-Alive");

    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);

    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

    dos.writeBytes(twoHyphens + boundary + lineEnd);
    // Log.d(TAG, "  dos1: " + dos);
    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""+ "file.png" + "\"" + lineEnd);
    // Log.d(TAG, "  dos2: " + dos);
    dos.writeBytes(lineEnd);
    // Log.d(TAG, "  dos3: " + dos);

    // create a buffer of maximum size

    int bytesAvailable = fileInputStream.available();
    int maxBufferSize = 1024;
    int bufferSize = Math.min(bytesAvailable, maxBufferSize);
    byte[] buffer = new byte[bufferSize];

    // read file and write it into form...

    int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    Log.d(TAG, " BytesREad:" + bytesRead);
    while (bytesRead > 0) {
        Log.d(TAG, " BytesREad in while:" + bytesRead);
        dos.write(buffer, 0, bufferSize);
        // Log.d(TAG, "  dos3: " + dos);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        /*
         * dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary +
         * twoHyphens + lineEnd);
         */
    }

    // send multipart form data necesssary after file data...

    // dos.write(data);
    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    // Log.d(TAG, "  dos4 tostring: " + dos.toString());

    // close streams
    // Log.e(Tag,"File is written");
    // fileInputStream.close();
    dos.flush();
    // Log.d(TAG, "  dos5: " + dos.toString());
    InputStream is = conn.getInputStream();
    // retrieve the response from server
    int ch;

    StringBuffer b = new StringBuffer();
    while ((ch = is.read()) != -1) {
        b.append((char) ch);
        // Log.d(TAG, "  b: " + b.toString());
    }
    String s = b.toString();
    Log.i("Response", s);
    // Log.d(TAG, "   response:" + s);
    dos.close();

}

错误包括

10-20 04:16:26.658: ERROR/dalvikvm-heap(20213):8957952字节的外部分配对于此进程来说太大。10-20 04:16:26.658: ERROR/dalvikvm(20213):内存不足:堆Size=4551KB,Allocated=3039KB,位图Size=9496KB 10-20 04:16:26.658: ERROR/(20213):VM不让我们分配100字节

EN

回答 2

Stack Overflow用户

发布于 2010-10-20 21:11:46

我之前遇到过非常类似的问题,结果不是上传图片,而是因为我试图在活动中展示它。

票数 1
EN

Stack Overflow用户

发布于 2011-03-02 09:46:09

当我在PhoneKlone上工作的时候,我发现上传大照片会导致android上的错误,这仅仅是因为虚拟机的错误限制。

解决方法:要么使用压缩来减小文件大小,要么尝试“阻止”图像(分多个步骤上传视频的裁剪部分)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3977513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档