gpt4 book ai didi

android - 使用android相机捕获视频后的空指针

转载 作者:行者123 更新时间:2023-11-30 01:36:09 25 4
gpt4 key购买 nike

我正在尝试在创建应用程序文件夹名称和 SD 卡中捕获视频。当我点击相机时,它会捕捉视频,但当我点击保存按钮时,我的应用程序崩溃了

public class MainActivity extends AppCompatActivity {

private Uri fileUri;
public static final int MEDIA_TYPE_VIDEO = 2;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
public static MainActivity ActivityContext =null;
public static TextView output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


ActivityContext = this;

Button buttonRecording = (Button)findViewById(R.id.recording);
output = (TextView)findViewById(R.id.output);

buttonRecording.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View arg0) {

// create new Intentwith with Standard Intent action that can be
// sent to have the camera application capture an video and return it.
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

// create a file to save the video
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);

// set the image file name
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

// set the video image quality to high
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

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





}
});



}
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){

return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){

// Check that the SDCard is mounted
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraVideo");


// Create the storage directory(MyCameraVideo) if it does not exist
if (! mediaStorageDir.exists()){

if (! mediaStorageDir.mkdirs()){

output.setText("Failed to create directory MyCameraVideo.");

Toast.makeText(ActivityContext, "Failed to create directory MyCameraVideo.",
Toast.LENGTH_LONG).show();

Log.d("MyCameraVideo", "Failed to create directory MyCameraVideo.");
return null;
}
}


// Create a media file name

// For unique file name appending current timeStamp with file name
java.util.Date date= new java.util.Date();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(date.getTime());

File mediaFile;

if(type == MEDIA_TYPE_VIDEO) {

// For unique video file name appending current timeStamp with file name
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");

} else {
return null;
}

return mediaFile;
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// After camera screen this code will excuted

if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {

if (resultCode == RESULT_OK) {

output.setText("Video File : " +data.getData());

// Video captured and saved to fileUri specified in the Intent
// Toast.makeText(this, "Video saved to:
// " +
// data.getData(), Toast.LENGTH_LONG).show();

} else if (resultCode == RESULT_CANCELED) {

output.setText("User cancelled the video capture.");

// User cancelled the video capture
Toast.makeText(this, "User cancelled the video capture.",
Toast.LENGTH_LONG).show();

} else {

output.setText("Video capture failed.");

// Video capture failed, advise user
Toast.makeText(this, "Video capture failed.",
Toast.LENGTH_LONG).show();
}
}
}
}

这是我的日志猫错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference

最佳答案

在你的 onActivityResult() 方法中

你写代码行的地方

    output.setText("Video File : " +data.getData()); 

这就是应用程序崩溃的原因,请更改上面的代码行以及您在其下方评论的行,更改如下:

    output.setText("Video File: "+fileUri.getPath());

Toast.makeText(this,"Video saved to: "+fileUri.getPath(),Toast.LENGTH_LONG).show();

关于android - 使用android相机捕获视频后的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35092359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com