Android 相机应用程序:java.lang.RuntimeException:无法恢复 activity
Android Camera App : java.lang.RuntimeException: Unable to resume activity
我正在开发一个应用程序,使用户能够按下按钮并录制视频,每当他按下停止时,视频就会被保存,然后视频的缩略图会显示在 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.access0(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.access0(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.access0(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)
你知道为什么会这样吗?我错过了什么吗?
您的 Activity
似乎由于某种原因被摧毁了,因此此时您的 fileUri
将是 null
。
我会在你的 onActivityResult
:
中做这样的事情
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
String path = fileUri.getPath().toString();
此外,在某些情况下,您会在您的方法中返回 null
,因此无论如何您都需要进行空检查。
我正在开发一个应用程序,使用户能够按下按钮并录制视频,每当他按下停止时,视频就会被保存,然后视频的缩略图会显示在 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.access0(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.access0(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.access0(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)
你知道为什么会这样吗?我错过了什么吗?
您的 Activity
似乎由于某种原因被摧毁了,因此此时您的 fileUri
将是 null
。
我会在你的 onActivityResult
:
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
String path = fileUri.getPath().toString();
此外,在某些情况下,您会在您的方法中返回 null
,因此无论如何您都需要进行空检查。