Reputation: 3457
I convert the video to base64 format in this way:
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();
}
}
I write the service code like this way:
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;
}
I got the error message as
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)
Upvotes: 1
Views: 728
Reputation: 49976
You can try catching it in Application.onlowMemory - but what will this get you? If your videos are huge then you should not use ByteArrayOutputStream but some kind of streaming method, ie. read chunk of video data, convert it, and then send it further. Or convert it but put results to sd card memory. Putting whole video in ByteArrayOutputStream will always end with OutOfMemory.
Upvotes: 2