Android:点击保存后相机崩溃

标签 android video-capture android-mediarecorder

我一直在开发一个具有录像机功能的应用程序。该应用程序运行正常,但在完成视频录制并单击“保存”按钮后,该应用程序崩溃并显示消息“不幸的是,应用程序已停止”。但是录制的视频按编码保存在文件夹中。有人可以帮我解决这个问题,因为我找不到错误在哪里。我的编码如下:

主要 Activity .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();
    }
}
}

任何帮助将不胜感激。谢谢。

日志:

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)

最佳答案

问题出在您代码的 onActivityResult 函数中调用 fileUri = data.getData();。调用应该是 data.getExtras().get("data");,它将获取 intent 中传递的实际数据。

请参阅answer by David Wasser以获得更多说明。

关于Android:点击保存后相机崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33664573/

相关文章:

android - 在 flutter 中仅显示图像的顶部部分

java - 覆盖锁定屏幕上的硬件音量按钮(设备处于唤醒状态)

android - 如何更改数组适配器中包含的 TextView 中的值

opencv - 更改 OpenCV 视频文件分辨率

android - 10 秒后 MediaRecorder.finalize() 中的 TimeoutException

android - MediaRecorder.setVideoFrameRate() 没有任何效果

android - 向可绘制形状添加文本

ios - 从cv::VideoCapture抓取最后一帧

android - 在OpenCV2.3.1中使用VideoCapture捕获android相机

Android 语音通话录音问题....无法与 audiosource.voicecall 一起正常工作