Android大视频上传到php服务器,我在stackoverflow中读了很多答案。
uploading video upto 50MB to the server
Android: OutOfMemoryError while uploading video - how best to chunk?
How to send HTTP POST request and receive response?
我试过了,但即使使用setChunkedStreamingMode()
也无法使用..当我上传视频时,它会给我OutOfMemoryError
当我录制视频5秒时,它的效果不错,但由于OutOfMemoryError
我的代码::
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(getString(R.string.login_url)
+ "uploadvideo");
HttpResponse response = null;
InputStream inputStream = getContentResolver().openInputStream(
Uri.fromFile(myFile));
byte[] data = IOUtils.toByteArray(inputStream);
String type = getFileExtension(myFile.getName().toString());
String encodeurl = Base64.encodeBytes(data);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
6);
nameValuePairs.add(new BasicNameValuePair("returnformat",
"json"));
nameValuePairs.add(new BasicNameValuePair("userid",
strUserid));
nameValuePairs.add(new BasicNameValuePair("video",
data.toString()));
nameValuePairs.add(new BasicNameValuePair("type", type));
nameValuePairs.add(new BasicNameValuePair("title",
strVideoName));
nameValuePairs.add(new BasicNameValuePair("description",
strVideoComments));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
System.out.println(">>>>>>>>>>" + response.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我需要在Base64 Encoded String
上传视频。而且我知道内存错误是因为内存比Heap-size更多。但我无法上传视频。
logcat的
05-31 23:16:12.114: E/AndroidRuntime(17255): java.lang.RuntimeException: An error occured while executing doInBackground()
05-31 23:16:12.114: E/AndroidRuntime(17255): at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.lang.Thread.run(Thread.java:1027)
05-31 23:16:12.114: E/AndroidRuntime(17255): Caused by: java.lang.OutOfMemoryError: (Heap Size=20103KB, Allocated=15700KB, Bitmap Size=0KB)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.nio.CharArrayBuffer.<init>(CharArrayBuffer.java:43)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.nio.ReadWriteCharArrayBuffer.<init>(ReadWriteCharArrayBuffer.java:47)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.nio.BufferFactory.newCharBuffer(BufferFactory.java:82)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.nio.CharBuffer.allocate(CharBuffer.java:53)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.nio.charset.CharsetDecoder.allocateMore(CharsetDecoder.java:261)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:218)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.nio.charset.Charset.decode(Charset.java:488)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.lang.String.<init>(String.java:181)
05-31 23:16:12.114: E/AndroidRuntime(17255): at java.lang.String.<init>(String.java:141)
答案 0 :(得分:2)
您不应该为实体设置大字节[]。它必然会耗尽内存。你可以在这里使用FileEntity。
http://developer.android.com/reference/org/apache/http/entity/FileEntity.html
一些相关的问题和链接
答案 1 :(得分:-5)
如果OutOfMemory错误来自服务器,快速解决方法是更新php.ini变量:
upload_max_filesize = 100M
(或任何你觉得舒服的事情)
或者如果你正在发帖,那么你也想要改变
post_max_size = 100M