【问题标题】:How to convert video file(.mp4) format into binary format in android?如何在android中将视频文件(.mp4)格式转换为二进制格式?
【发布时间】:2012-11-17 05:29:25
【问题描述】:

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

我尝试在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);

        }
    });


}

}

【问题讨论】:

  • 您的 Web 服务是采用哪种技术开发的?
  • 如何将我的视频文件转换为二进制格式以将请求发送到服务器以保存文件..?
  • 我尝试使用上述编码...我得到了二进制格式..
  • 如何在 Web 服务器中传递二进制格式的值...?
  • 我在 for 循环中遇到错误并在执行时强制关闭...

标签: java android video web


【解决方案1】:

我在我的 3 个应用程序中体验到,如果使用 XML 通过服务器发送 IMAGE 或 VIDEO 是很好的。 如果您想在 ANDROID 中上传 IMAGE 或 VIDEO,最好以 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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-10
    • 2023-01-17
    • 2016-05-01
    • 2012-01-20
    • 2011-12-05
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多