我通过以下方式将视频转换为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)

最佳答案

您可以尝试在Application.onlowMemory中捕获它-但这能为您带来什么?如果视频很大,则不应使用ByteArrayOutputStream,而应使用某种流传输方法,即。读取视频数据块,进行转换,然后进一步发送。或将其转换但将结果存入SD卡存储器。将整个视频放入ByteArrayOutputStream将始终以OutOfMemory结尾。

关于android - 如何在Android中处理OutOfMemoryError?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12543987/

10-09 08:25