如何在android中将视频文件(.mp4)格式转换为二进制格式?

时间:2012-11-17 05:29:25

标签: java android video web

我想在网络服务器上传视频。我得到了服务,我想以二进制格式传递文件,我该怎么做?

我试图在base64的帮助下将视频文件转换为二进制格式。?

public class binaryformat extends Activity {
private String strAttachmentCoded;
Button b1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b1=(Button)findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
             File file = new File("/mnt/sdcard/C:/Program Files (x86)/Wowza Media Systems/Wowza Media Server 3.1.2/content/sample.mp4");
                FileInputStream objFileIS = null;
                try
                {
                    objFileIS = new FileInputStream(file);
                } 
                catch (FileNotFoundException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream();
                byte[] byteBufferString = new byte[1024];
                try
                {
                    for (int readNum; (readNum = objFileIS.read(byteBufferString)) != -1;) 
                    {
                     objByteArrayOS.write(byteBufferString, 0, readNum);
                     System.out.println("read " + readNum + " bytes,");
                    }
                } 
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }                    
                byte[] byteBinaryData = Base64.encode((objByteArrayOS.toByteArray()), Base64.DEFAULT);
               strAttachmentCoded = new String(byteBinaryData);

        }
    });


}

}

1 个答案:

答案 0 :(得分:0)

我在我的3应用程序中遇到过使用XML通过服务器发送IMAGE或VIDEO是好的。 如果要在ANDROID中上传图像或视频,最好以XML格式在base64中发送IMAGE或VIDEO。

public static String uploadMultiplePhoto(String url, String xmlString) {
        String responseString = "";
        try {
            //instantiates httpclient to make request
            DefaultHttpClient httpclient = new DefaultHttpClient();
            //url with the post data
            HttpPost request = new HttpPost(url);
            //convert parameters into JSON object
            //JSONObject holder = new JSONObject(jsonObjString);

            //passes the results to a string builder/entity
            StringEntity se = new StringEntity(xmlString);
            //sets the post request as the resulting string
            request.setEntity(se);
            //sets a request header so the page receving the request
            //will know what to do with it
            request.setHeader("Accept", "application/xml");
            /*request.setHeader("Content-type", "application/xml");*/

            //Handles what is returned from the page
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            responseString = httpclient.execute(request, responseHandler);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return responseString;
    }