tmp

 private static final int MEDIA_TYPE_VIDEO = 2;   


 private String getRealFilePath(final Uri uri) {

        if ( null == uri ) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (null == scheme){
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        }
        return data;


    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        if (resultCode == RESULT_OK && null != data) {
            switch (requestCode) {
            case request_take_video_Code:
                mUri = data.getData();
                if (null == mUri) return;
                mVideoPath = getAbsolutePath(mUri);
                showImage();
                break;
                
            case request_record_video_Code:
                mUri = data.getData();
                if (null == mUri) return;
                
                //mVideoPath = getAbsolutePath(mUri);
                Log.d(TAG, "onActivityResult request_record_video_Code mUri=" + mUri + " mUri.getPath()=" + mUri.getPath());
                mVideoPath = getRealFilePath(mUri);
                Log.d(TAG, "mVideoFile mVideoPath="+ mVideoPath);
                showImage();
                break;
            }
        }
        
    }
    
    /** Create a file Uri for saving an image or video */
    private Uri getOutputMediaFileUri(final int type){
        mVideoFile = getOutputMediaFile(type);
        if (null == mVideoFile) {
            return null;
        }
        return Uri.fromFile(mVideoFile);
    }
    
    /** Create a File for saving an image or video */
    private File getOutputMediaFile(final int type){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.


        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "MyCameraApp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.


        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()){
            if (!mediaStorageDir.mkdirs()){
                Log.d(TAG, "failed to create directory");
                return null;
            }
        }


        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile = null;
        if(MEDIA_TYPE_VIDEO == type) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }
        return mediaFile;
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn:
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("video/*");
            startActivityForResult(Intent.createChooser(intent, "Select movie"), request_take_video_Code);
            break;
            
        case R.id.btn_record:
            Intent it = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video
            it.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the video file name
            it.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 10);  // 10MB
            it.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 5); //5s
            startActivityForResult(it, request_record_video_Code);
            break;
        }
    } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值