如何修复 Android 照片上传以发送更大的数据流

发布于 2024-09-28 03:28:31 字数 3197 浏览 7 评论 0原文

我有一个 Android 应用程序正在尝试通过 API 接口将照片上传到网络服务器。只要图像尺寸不太大(在 1280x800 左右失败),该方法就可以正常工作。下面的代码适用于较小的图像,但适用于较大的图像。有什么建议吗?

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:错误/dalvikvm(20213):内存不足:堆大小= 4551 KB,已分配= 3039 KB,位图大小= 9496 KB 10-20 04:16:26.658: 错误/(20213): VM 不允许我们分配 8957952 字节

I have an android app that is attempting to upload photos to a web server via API interface. The method works fine as long as the image sizes are not too big (fails somewhere around 1280x800) Heres the code that will work will smaller images but fails with larger ones. Any suggestions?

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

}

The errors include

10-20 04:16:26.658: ERROR/dalvikvm-heap(20213): 8957952-byte external allocation too large for this process.
10-20 04:16:26.658: ERROR/dalvikvm(20213): Out of memory: Heap Size=4551KB, Allocated=3039KB, Bitmap Size=9496KB
10-20 04:16:26.658: ERROR/(20213): VM won't let us allocate 8957952 bytes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够爱げ你 2024-10-05 03:28:31

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

I had very similar problem before, turned out not to be uploading the picture but because I was trying to display it on an activity..

旧时模样 2024-10-05 03:28:31

在开发 PhoneKlone 时,我发现上传大照片会在 Android 上导致错误,原因仅仅是因为虚拟机错误限制。

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

While working on PhoneKlone, I found that uploading large photos caused errors on android simply because of the vm error limit.

Workaround: either use compression to reduce the file size or try 'blocking' the image (uploading cropped sections of the video in multiple steps)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文