Android 使用HttpURLConnection导线上载超过5 MB内存不足错误
我需要的解决方案,如何上传超过5 MB的服务器,因为它可以上传周围 5MB,而且我在Stackoverflow中搜索了许多线程,我无法获得使用HttpURLConnection上传超过5MB的解决方案 我已经使用下面的代码写入流Android 使用HttpURLConnection导线上载超过5 MB内存不足错误,android,file-upload,httpurlconnection,Android,File Upload,Httpurlconnection,我需要的解决方案,如何上传超过5 MB的服务器,因为它可以上传周围 5MB,而且我在Stackoverflow中搜索了许多线程,我无法获得使用HttpURLConnection上传超过5MB的解决方案 我已经使用下面的代码写入流 long lenghtOfFile = file2.length(); long total = 0; try { FileInputStre
long lenghtOfFile = file2.length();
long total = 0;
try {
FileInputStream is = null;
java.nio.ByteBuffer bb = null;
try {
is = new FileInputStream(file2);
java.nio.channels.FileChannel fc = is.getChannel();
bb = java.nio.ByteBuffer.allocate(10000);
byte[] bytes;
while (fc.read(bb) >= 0) {
bb.flip();
// save the part of the file into a chunk
bytes = bb.array();
bb.clear();
Log.i("ByteLength", "" + bb.array().length);
output.write(bytes, 0, bytes.length);
total += bytes.length;
Log.i("total", "" + lenghtOfFile);
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.flush();
System.gc();
}
} catch (Exception e) {
} finally {
try {
is.close();
output.flush();
bb.clear();
} catch (Exception e) {
}
}
} catch (Exception e) {
}
请检查这个片段,并给我解决这个问题的方法 上传建议之前,您可以使用:
HttpURLConnection.setFixedLengthStreamingMode(int size)
方法以确保上载文件的大小
ps:请参阅api文档:
我在这一行输出时得到内存不足错误。写入(字节,0,字节。长度);尝试拆分为段落,这里的每一段我都将视频文件阵列上传到服务器,每一段至少有7MB,而且在上传所有文件之前,我无法关闭编写器。我得到的异常是12-27 14:42:14.485:ERROR/AndroidRuntime(28579):在java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:93)12-27 14:59:16.345:ERROR/AndroidRuntime(29230):在java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)帮助我解决brian..我在这行输出中遇到内存不足错误。write(bytes,0,bytes.length)。我已经按照您所说的设置了10MB的HttpURLConnection.setFixedLengthStreamingMode(10*1024*1024),但仍然得到了错误