我一直在开发具有录像机功能的应用程序。该应用程序工作正常但在完成录制视频并单击“保存”按钮后,应用程序崩溃并显示消息“不幸的是,应用程序已停止”。但录制的视频会以编码方式保存在文件夹中。有人可以帮助我解决这个问题,因为我无法找到错误的位置。我的编纂如下:
MainActivity.java:
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;
import java.io.File;
import java.text.SimpleDateFormat;
public class MainActivity extends Activity {
final static int REQUEST_VIDEO_CAPTURED = 1;
VideoView videoviewPlay;
private Uri fileUri;
public static final int MEDIA_TYPE_VIDEO = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonRecording = (Button) findViewById(R.id.recording);
buttonRecording.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.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);
startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);
}
});
}
/** 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.getExternalStorageDirectory().getAbsolutePath() + "/Do This Video/");
// Create the storage directory(Do This Video) if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("Do This Video", "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) {
// TODO Auto-generated method stub
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_VIDEO_CAPTURED){
fileUri = data.getData();
Toast.makeText(MainActivity.this,
fileUri.getPath(),
Toast.LENGTH_LONG)
.show();
}
}else if(resultCode == RESULT_CANCELED){
fileUri = null;
Toast.makeText(MainActivity.this,
"Cancelled!",
Toast.LENGTH_LONG)
.show();
}
}
}
非常感谢任何帮助。谢谢。
logcat的:
11-13 13:57:54.538 27336-27336/com.example.dothis.video I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
11-13 13:57:54.538 27336-27336/com.example.dothis.video I/SELinux﹕ Function: selinux_android_load_priority [1], There is no sepolicy version file.
11-13 13:57:54.538 27336-27336/com.example.dothis.video I/SELinux﹕ Function: selinux_android_load_priority , priority version is VE=GOOGLE_POLICY
11-13 13:57:54.538 27336-27336/com.example.dothis.video I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
11-13 13:57:54.543 27336-27336/com.example.dothis.video D/dalvikvm﹕ Late-enabling CheckJNI
11-13 13:57:54.693 27336-27336/com.example.dothis.video D/TextLayoutCache﹕ Enable myanmar Zawgyi converter
11-13 13:57:54.723 27336-27336/com.example.dothis.video D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
11-13 13:57:54.723 27336-27336/com.example.dothis.video D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
11-13 13:57:54.733 27336-27336/com.example.dothis.video D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
11-13 13:57:54.738 27336-27336/com.example.dothis.video E/﹕ Device driver API match
Device driver API version: 23
User space API version: 23
11-13 13:57:54.738 27336-27336/com.example.dothis.video E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Mar 21 13:52:50 KST 2014
11-13 13:57:54.813 27336-27336/com.example.dothis.video D/OpenGLRenderer﹕ Enabling debug mode 0
11-13 13:57:54.833 27336-27336/com.example.dothis.video D/TextLayoutCache﹕ Enable myanmar Zawgyi converter
11-13 13:57:57.018 27336-27336/com.example.dothis.video W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
11-13 13:58:07.848 27336-27336/com.example.dothis.video D/AndroidRuntime﹕ Shutting down VM
11-13 13:58:07.848 27336-27336/com.example.dothis.video W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41e23c08)
11-13 13:58:07.853 27336-27336/com.example.dothis.video E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.dothis.video, PID: 27336
java.lang.RuntimeException: Unable to resume activity {com.example.dothis.video/com.example.dothis.video.AndroidVideoCapture}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=null} to activity {com.example.dothis.video/com.example.dothis.video.AndroidVideoCapture}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3076)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3105)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4054)
at android.app.ActivityThread.access$1000(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=200, result=-1, data=null} to activity {com.example.dothis.video/com.example.dothis.video.AndroidVideoCapture}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3681)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3063)
答案 0 :(得分:1)
你不能通过getPath()简单地获取路径,你应该检查数据或data.getData是否为空。
if(requestCode == REQUEST_VIDEO_CAPTURED){
if(data == null || data.getData() ==null){
//Log.e();
return;
}
fileUri = data.getData();
String filepath = uritofilpath(fileUri);
}
的getPath:
public static String getPath(Uri uri,Context ctx) {
String res = null;
if(null==uri){
return res;
}
if (uri != null && uri.toString().startsWith("file://")) {
return uri.toString().substring("file://".length());
}
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = ctx.getContentResolver().query(uri, proj, null, null, null);
if(cursor!=null){
if(cursor.moveToFirst()){
try {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}catch (Exception ignored){
}finally {
closeCursor(cursor);
}
}
}
closeCursor(cursor);
return res;
}
答案 1 :(得分:1)
问题出在您的代码的fileUri = data.getData();
函数中的onActivityResult
调用中。调用应为data.getExtras().get("data");
,这将获得intent
。
有关详细说明,请参阅answer by David Wasser。