Android 视频流上传到 php 服务器?

标签 android android-webview android-video-player

你好,

任何人都建议一些示例和想法将视频数据发布到服务器。我想稍后直接从​​服务器播放视频作为流视频。任何人都建议我如何将流视频上传到 php 服务器并将其作为流视频播放来自服务器。

最佳答案

muniir@msn.com 写道:

public class SocialHTTPPost 
{ 
    public static byte[] getCapturedImageStream(Context ctx, Intent data)
    {
        String fileName = ((MainActivity) ctx).mCaptureImageHMap.get("name").toString();
        int mid= fileName.lastIndexOf(".");
        String ext=fileName.substring(mid+1,fileName.length());

        Bundle b = data.getExtras();
        Bitmap bmp = (Bitmap) b.get("data");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        if(ext.equalsIgnoreCase("png"))
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
        else if(ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("jpeg"))
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] bytes = stream.toByteArray();        
        return bytes;
    }

    public static void postImage(Context ctx, byte[] ba)
    {       
        String sid = ((MainActivity) ctx).mCaptureImageHMap.get("sid").toString();

        String ba1=Base64.encode(ba);
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("image",ba1));

        for (Iterator iter = ((MainActivity) ctx).mCaptureImageHMap.entrySet().iterator(); iter.hasNext();) 
        {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            nameValuePairs.add(new BasicNameValuePair(key, value));
        }

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(Config.MEDIA_IMAGE_PATH + "?AddFile=AddFile&SessionId=" + sid);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            //is = entity.getContent();
        }
        catch(Exception e)
        {
            CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString());
            CommonFunctions.showToast(ctx, "Unable to post captured image file: " + e.toString());
        }       
    }

    public static byte[] getCapturedVideoStream(Context ctx, Intent data)
    {       
        try 
        {
            AssetFileDescriptor videoAsset = ctx.getContentResolver().openAssetFileDescriptor(data.getData(), "r");
            FileInputStream fis = videoAsset.createInputStream();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            try 
            {
                for (int readNum; (readNum = fis.read(buf)) != -1;)                
                    bos.write(buf, 0, readNum);
            } 
            catch (IOException e) 
            {
                CommonFunctions.writeLOG(CacheImagesManager.class.getClass().toString(), e.toString());
            }
            byte[] bytes = bos.toByteArray();
            return bytes;
        } 
        catch (IOException e) 
        {
            CommonFunctions.writeLOG(CacheImagesManager.class.getClass().toString(), e.toString());
            return null;
        }       
    }

    public static void postVideo(Context ctx, byte[] ba)
    {       
        String sid = ((MainActivity) ctx).mCaptureVideoHMap.get("sid").toString();

        String ba1=Base64.encode(ba);
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("video",ba1));

        for (Iterator iter = ((MainActivity) ctx).mCaptureVideoHMap.entrySet().iterator(); iter.hasNext();) 
        {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            nameValuePairs.add(new BasicNameValuePair(key, value));
        }

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(Config.MEDIA_VIDEO_PATH + "?AddFile=AddFile&SessionId=" + sid);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            //HttpEntity entity = response.getEntity();
        }
        catch(Exception e)
        {
            CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString());
            CommonFunctions.showToast(ctx, "Unable to post captured video file: "  + e.toString());
        }       
    }

    //  //////////////////////////////////////////  SAMPLE TEST START FOR READING WRITING IMAGES AND VIDEO BINARY

    public static boolean writeCaptureVideo(Context ctx, Intent data)
    {       
        String fileName = ((MainActivity) ctx).mCaptureImageHMap.get("name").toString();
        String sid = ((MainActivity) ctx).mCaptureImageHMap.get("sid").toString();
        String path = ""; //((MainActivity) ctx).mMediaPath;

        try 
        {
            AssetFileDescriptor videoAsset = ctx.getContentResolver().openAssetFileDescriptor(data.getData(), "r");
            FileInputStream fis = videoAsset.createInputStream();           
            File tmpFile = new File(path, fileName); 
            FileOutputStream fos = new FileOutputStream(tmpFile);
            byte[] buf = new byte[1024];
            int len;
            while ((len = fis.read(buf)) > 0) 
            {
                fos.write(buf, 0, len);
            }       
            fis.close();
            fos.close();
            return true;
        } 
        catch (IOException e) 
        {
            CommonFunctions.writeLOG(CacheImagesManager.class.getClass().toString(), e.toString());
            return false;
        }       
    }

    public static boolean writeCaptureImage(Context ctx, Intent data)
    {
        String fileName = ((MainActivity) ctx).mCaptureImageHMap.get("name").toString();
        String path = ""; //((MainActivity) ctx).mMediaPath;

        int mid= fileName.lastIndexOf(".");
        String ext=fileName.substring(mid+1,fileName.length());
        File file = new File(path, fileName);
        Uri outputFileUri = Uri.fromFile( file );
        Bundle b = data.getExtras();
        Bitmap bm = (Bitmap) b.get("data");

        try 
        {               
            if(ext.equalsIgnoreCase("png"))
                bm.compress(CompressFormat.PNG, 100, new FileOutputStream(file));
            else if(ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("jpeg"))
                bm.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));
            try {
                Runtime.getRuntime().exec("chmod 777 " + path + "/" + fileName);
            } catch (IOException e1){}

            return true;
        } 
        catch (FileNotFoundException e) 
        {
            CommonFunctions.writeLOG(CacheImagesManager.class.getClass().toString(), e.toString());
            return false;
        }       
    }

    public static void postImage2(Context ctx)
    {
        //help site:    http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/
        String fileName = ((MainActivity) ctx).mCaptureImageHMap.get("name").toString();
        String sid = ((MainActivity) ctx).mCaptureImageHMap.get("sid").toString();
        String path = ""; //((MainActivity) ctx).mMediaPath;

        int mid= fileName.lastIndexOf(".");             
        String ext=fileName.substring(mid+1,fileName.length());

        //InputStream is;
        Bitmap bitmapOrg = BitmapFactory.decodeFile(path + "/" + fileName);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();

        if(ext.equalsIgnoreCase("png"))
            bitmapOrg.compress(Bitmap.CompressFormat.PNG, 100, bao);
        else if(ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("jpeg"))
            bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);

        byte [] ba = bao.toByteArray();
        String ba1=Base64.encode(ba);
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("image",ba1));

        for (Iterator iter = ((MainActivity) ctx).mCaptureImageHMap.entrySet().iterator(); iter.hasNext();) 
        {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            nameValuePairs.add(new BasicNameValuePair(key, value));
        }       

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(Config.MEDIA_IMAGE_PATH + "?AddFile=AddFile&SessionId=" + sid );
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            //is = entity.getContent();
        }
        catch(Exception e)
        {
            CommonFunctions.writeLOG(ctx.getClass().toString(), e.toString());
            CommonFunctions.showToast(ctx, "Unable to post captured image file: " + fileName);
        }       
    }

    //  //////////////////////////////////////////  SAMPLE TEST END
}

关于Android 视频流上传到 php 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5076870/

相关文章:

android - 启动 Android 应用程序时 Eclipse 挂起

Android WebView 在第一次页面完成时没有从本地存储读取数据?

JavaScriptInterface() 在 JellyBean 中不起作用

android - 有没有人看过 Android 应用程序使用嵌入式播放器播放 youtube 视频?

android - 视频播放器开始播放时如何暂停音频播放器?

android - 安卓应用测试录音工具

java - Android 使用 indexOf

android - 根据 exoplayer 演示应用程序中视频的纵横比更改表面 View 的纵横比

java - 是否有任何方法或参数可以从 stripe api 获取无效/停止卡信息 我只能从 stripe 获取到期月份/年份

android - 离开 WebView 后如何停止 Flash?