Android大视频上传到php服务器,我在stackoverflow中阅读了很多答案。
Android:上传视频时出现 OutOfMemoryError - 如何最好地分块?
我试过了,但即使使用它也无法为我工作。当我setChunkedStreamingMode()
上传视频时,它给了我OutOfMemoryError
当我录制 5 秒的视频时,它的效果很好,但超过 8 或 10 秒的视频应用程序强制关闭,因为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
。而且我知道内存错误是由于内存大于堆大小。但我无法上传视频。
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)