我创建了一个这样的应用程序。 它有一个按钮,点击按钮即可开始视频捕捉。
现在我有一个这样的代码: -
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;
}
}
现在,在我捕获视频后,应用程序崩溃了。
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
基本上问题是那个 在代码中
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
这里没有将文件保存到位置集,我不知道这是什么问题。 我从此链接http://developer.android.com/guide/topics/media/camera.html
中获取了此代码答案 0 :(得分:5)
否如果您通过Uri
传递putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
以使用data.getData();
保存图片或视频,那么您始终会在onActivityResult
中收到NULL
Uri
。因此,使用@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();
}
}
}
图像或视频传递的意图为:
{{1}}
答案 1 :(得分:2)
private static String FILE_NAM1 = "video_one";
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == VIDEO_ACTIVITY) {
Uri videoFileUri = data.getData();
String outputfile = getFilesDir() + File.separator+FILE_NAM1+"_tmp.mp4";
try {
InputStream in = getContentResolver().openInputStream(videoFileUri);
MainActivity.createFileFromInputStream(in, outputfile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("saved uri is "+videoFileUri.toString());
Intent intent = new Intent(getBaseContext(), VideoActivity.class);
intent.putExtra("video_uri",videoFileUri);
startActivity(intent);
}
}
private static File createFileFromInputStream(InputStream inputStream, String fileName) {
try{
File f = new File(fileName);
f.setWritable(true, false);
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;
while((length=inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}
outputStream.close();
inputStream.close();
return f;
}catch (IOException e) {
System.out.println("error in creating a file");
e.printStackTrace();
}
return null;
}
删除intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);来自意图
答案 2 :(得分:0)
我使用此代码进行录制和保存视频:
private String name_vid;
private static int REC_VIDEO = 1;
public void rec_vid(View v) {
//set name & path to vid
name_vid = Environment.getExternalStorageDirectory()+"/videos/myvid.mp4";
//check if folder exists, else, create it
File path = new File(Environment.getExternalStorageDirectory()+"/videos/");
if (!path.exists())
path.mkdirs();
//create the intent
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
int code = REC_VIDEO;
Uri output = Uri.fromFile(new File(name_vid));
//Add extra to save video
intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
startActivityForResult(intent, code);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REC_VIDEO) {
if (resultCode == -1) {
//create a MediaScanner to save the vid into the indicate path
new MediaScannerConnectionClient() {
private MediaScannerConnection msc = null;
{
msc = new MediaScannerConnection(
getApplicationContext(), this);
msc.connect();
}
public void onMediaScannerConnected() {
msc.scanFile(name_vid, null);
}
public void onScanCompleted(String path, Uri uri) {
msc.disconnect();
}
};
Toast.makeText(this,"Video saved succesfull",
Toast.LENGTH_SHORT).show();
}
}
}
请记住,您必须在清单中设置权限使用:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />