我正在开发一个应用程序,使用户可以按下按钮并录制视频,每当他按下停止时,视频都会保存,然后视频的缩略图会显示在ImageView中。这是源代码:
public class Home extends ActionBarActivity {
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
private Uri fileUri;
public static final int MEDIA_TYPE_VIDEO = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button openCam = (Button) findViewById(R.id.button);
openCam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//send request to capture video
Intent myVideo = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//video save file
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
myVideo.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
//set high quality
myVideo.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
//set camera roll to 25sec length
myVideo.putExtra(MediaStore.EXTRA_DURATION_LIMIT,25);
//execute intent, camera opens for the user to capture vid
startActivityForResult(myVideo, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
ImageView imgView = (ImageView) findViewById(R.id.imageView);
TextView tv = (TextView) findViewById(R.id.textView);
TextView tv2 = (TextView) findViewById(R.id.textView2);
String path = fileUri.getPath().toString(); <-- line 76
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path,
MediaStore.Images.Thumbnails.MINI_KIND);
imgView.setImageBitmap(thumb);
tv.setVisibility(View.VISIBLE);
tv2.setVisibility(View.VISIBLE);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
} else {
//Video capture failed, advise user
}
}
}
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
//create file to store video
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES), "MyCameraApp");
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
当我在我的模拟器(Genymotion)上启动我的应用程序时,一切都按预期的方式工作,但是当我在真正的Android设备上运行时,应用程序强制关闭用户停止录制的那一分钟。
这是我从Logat获得的输出:
03-17 15:23:06.379 20159-20159/com.example.manolis.mrw E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.manolis.mrw, PID: 20159
java.lang.RuntimeException: Unable to resume activity {com.example.manolis.mrw/com.example.manolis.mrw.Home}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=Intent { dat=file:///storage/emulated/0/Movies/MyCameraApp/VID_20150317_152258.mp4 }} to activity {com.example.manolis.mrw/com.example.manolis.mrw.Home}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2909)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=Intent { dat=file:///storage/emulated/0/Movies/MyCameraApp/VID_20150317_152258.mp4 }} to activity {com.example.manolis.mrw/com.example.manolis.mrw.Home}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3499)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2867)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2909)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.manolis.mrw.Home.onActivityResult(Home.java:76)
at android.app.Activity.dispatchActivityResult(Activity.java:5432)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3495)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2867)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2909)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2306)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
你知道为什么会这样吗?我错过了什么吗?
答案 0 :(得分:1)
似乎您的Activity
因某种原因被销毁,因此此时fileUri
将为null
。
我会在onActivityResult
:
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
String path = fileUri.getPath().toString();
此外,在某些情况下,您将在方法上返回null
,因此无论如何都需要进行空检查。