gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:31:08 25 4
gpt4 key购买 nike

我有一个 Android 应用程序试图通过 API 接口(interface)将照片上传到 Web 服务器。只要图像尺寸不太大(在 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):内存不足:堆大小=4551KB,分配=3039KB,位图大小=9496KB10-20 04:16:26.658: ERROR/(20213): VM 不允许我们分配 8957952 字节

最佳答案

我之前遇到过非常相似的问题,原来不是上传图片而是因为我试图在 Activity 中显示它..

关于android - 如何修复我的 android 照片上传以发送更大的数据流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3977513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com