INTENT.ACTION_VIDEO_CAPTURE不保存文件到自定义位置乌里 [英] INTENT.ACTION_VIDEO_CAPTURE not saving file to custom location Uri

查看:261
本文介绍了INTENT.ACTION_VIDEO_CAPTURE不保存文件到自定义位置乌里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个像这样的应用程序。
它有一个按钮,按钮的点击视频采集开始。

I have created an app like this. It has a Button , on the click of button the video capture starts.

现在我有一个code像这样的: -

now I have a code like this for it:-

public class VideoCaptureComponentActivity extends Activity implements View.OnClickListener {
    VideoView vv;
    ImageButton ib;
    TextView tv;
    Intent i;
    final static int cameraData=0;
    File path = null;
    Uri myVideo;

    private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
    private Uri fileUri;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);

        vv= (VideoView) findViewById(R.id.vvVideoCapture);
        ib=(ImageButton) findViewById(R.id.btnVideo);
        ib.setOnClickListener(this);
        tv= (TextView) findViewById(R.id.tvFilePath);
    }

    @Override
    public void onClick(View v) {               
        switch(v.getId())
        {
            case R.id.btnVideo:
                Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                fileUri = getOutputMediaFileUri();  // create a file to save the video
                intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); // set the video image quality to high

                // start the Video Capture Intent
                startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
                break;
        }

    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
     if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) 
     {
            if (resultCode == RESULT_OK) 
            {
                myVideo= data.getData();
                tv.setText(myVideo.toString());
                vv.setVideoURI(myVideo);
                vv.setMediaController(new MediaController(this));
                vv.requestFocus();
                vv.start(); 
            }           
    }
}


/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri()
{
      return Uri.fromFile(getOutputMediaFile());
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile()
{
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");

    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "VID_CAPTURED" + ".mp4");
   return mediaFile;
}


}

现在经过我为捕获应用程序崩溃的视频。

now after I m capturing the video the application is crashing.

07-06 17:12:09.447: E/AndroidRuntime(2917): FATAL EXCEPTION: main
07-06 17:12:09.447: E/AndroidRuntime(2917): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=Intent {  }} to activity {com.optimus.mobile.survey/com.optimus.mobile.survey.VideoCaptureComponentActivity}: java.lang.NullPointerException
07-06 17:12:09.447: E/AndroidRuntime(2917):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3712)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3754)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at android.app.ActivityThread.access$2800(ActivityThread.java:135)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2162)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at android.os.Looper.loop(Looper.java:143)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at android.app.ActivityThread.main(ActivityThread.java:4914)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at java.lang.reflect.Method.invokeNative(Native Method)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at java.lang.reflect.Method.invoke(Method.java:521)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at dalvik.system.NativeStart.main(Native Method)
07-06 17:12:09.447: E/AndroidRuntime(2917): Caused by: java.lang.NullPointerException
07-06 17:12:09.447: E/AndroidRuntime(2917):     at com.optimus.mobile.survey.VideoCaptureComponentActivity.onActivityResult(VideoCaptureComponentActivity.java:82)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at android.app.Activity.dispatchActivityResult(Activity.java:3931)
07-06 17:12:09.447: E/AndroidRuntime(2917):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3708)
07-06 17:12:09.447: E/AndroidRuntime(2917):     ... 11 more

基本上问题在于搜索
在code

basically the problem is that that
in the code

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri();  
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); 

这是不将文件保存到设定的位置,我不知道什么问题。
我把这个code从这个链接 http://developer.android.com /guide/topics/media/camera.html

推荐答案

没有,如果你是路过乌里保存图像或视频与 putExtra( MediaStore.EXTRA_OUTPUT,fileURI所); 那么你总是收到 data.getData(); 的onActivityResult NULL 。所以用图像的乌里或视频意图作为传递:

No if you are passing Uri for saving Image or Video with putExtra(MediaStore.EXTRA_OUTPUT, fileUri); then you always receive data.getData(); in onActivityResult as NULL. so use Uri of image or Video passed with intent as:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
     if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) 
     {
            if (resultCode == RESULT_OK) 
            {
              //  myVideo= data.getData();
                tv.setText("Video"); 
                vv.setVideoURI(fileUri);//set Uri here which you passed with Intent
                vv.setMediaController(new MediaController(this));
                vv.requestFocus();
                vv.start(); 
            }           
    }
}

这篇关于INTENT.ACTION_VIDEO_CAPTURE不保存文件到自定义位置乌里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆