我以这种方式将视频转换为base64格式:
if (requestCode == VIDEO_REQ) {
Uri vid = data.getData();
String videoPath = getRealPathFromURI(vid);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis = null;
try {
fis = new FileInputStream(new File(videoPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] buf = new byte[1024];
int n;
try {
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (IOException e) {
e.printStackTrace();
}
byte[] videoBytes = baos.toByteArray();
String test = Base64.encodeBytes(videoBytes);
JSONParsingXML jsonp=new JSONParsingXML();
try {
jsonp.imageUpload(test);
} catch (JSONException e) {
e.printStackTrace();
}
}
我这样编写服务代码:
public JSONObject imageUpload(String userid,String deviceid,String sessionid,String image){
JSONObject jsonResponse =null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("some url/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("userid",userid ));
nameValuePairs.add(new BasicNameValuePair("deviceid", deviceid));
nameValuePairs.add(new BasicNameValuePair("sessionkey",sessionid ));
nameValuePairs.add(new BasicNameValuePair("imagebase64", image));
nameValuePairs.add(new BasicNameValuePair("type", "video"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
System.out.println(httppost);
HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();//An entity that can be sent or received with an HTTP message. Entities can be found in some requests and in responses, where they are optional.
String changeTIDRec = EntityUtils.toString(responseEntity);
System.out.println(changeTIDRec);
Log.v("responseEntity",""+responseEntity);
jsonResponse = new JSONObject(changeTIDRec);//insert the response value into JSONObject.
Log.v("jsonResponse",""+jsonResponse);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
} catch(Exception e){
}
return jsonResponse;
}
我收到错误消息
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): FATAL EXCEPTION: main
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): java.lang.OutOfMemoryError
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:97)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:144)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at java.lang.StringBuffer.append(StringBuffer.java:126)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at java.net.URLEncoder.encode(URLEncoder.java:111)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at org.apache.http.client.utils.URLEncodedUtils.encode(URLEncodedUtils.java:184)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at org.apache.http.client.utils.URLEncodedUtils.format(URLEncodedUtils.java:163)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at org.apache.http.client.entity.UrlEncodedFormEntity.<init>(UrlEncodedFormEntity.java:71)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at com.player.webservices.WebServices.imageUpload(WebServices.java:323)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at com.player.jsonparsing.JSONParsingXML.imageUpload(JSONParsingXML.java:191)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at com.player.presentationlayer.CreateMessageActivity.onActivityResult(CreateMessageActivity.java:254)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at android.os.Looper.loop(Looper.java:123)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at java.lang.reflect.Method.invoke(Method.java:521)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-22 18:16:05.093: ERROR/AndroidRuntime(5455): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您可以尝试在Application.onlowMemory中捕获它 - 但是这会让您获得什么?如果您的视频很大,那么您不应该使用ByteArrayOutputStream,而是使用某种流式传输方法,即。读取视频数据块,转换它,然后进一步发送。或转换它但将结果放入SD卡内存。将整个视频放在ByteArrayOutputStream中将始终以OutOfMemory结束。