当前位置: 首页 > 知识库问答 >
问题:

Android系统:点击保存后相机崩溃

尉迟子民
2023-03-14

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

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)

            

共有1个答案

程和畅
2023-03-14

不能通过getPath()简单地获取path,应该检查data或data.getData是否为null。

   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;
    }
 类似资料:
  • 我是Android的新手。我正在我的Android应用程序中创建ImageUpload活动,但它在Apk(23)下运行良好。但每当我在AndroidMarshmallow上尝试它时,点击图片后它就会崩溃。 这是日志 java.lang.RuntimeException:将结果ResultInfo{who=null,request=1,result=-1,data=intent{}}传递到活动{co

  • 我可以按下一个按钮,打开原生相机app,成功拍下一张照片。但当我查看手机上的照片库或原生应用程序时,照片并没有保存在那里。我对Android很陌生,所以很可能我的代码中遗漏了一些重要的东西。 问题: 1)这些图片保存在哪里? 2)我是否可以修改下面的代码,以保存到内部存储,所以所有的图片拍摄与我的应用程序是私人的,只能通过我的应用程序访问? 3)如果我想将这些图片连同一些文本/其他输入一起保存到一

  • 我在谷歌游戏商店里有一个Android游戏,是用统一2018.4.2f1和IL2CPP而不是单声道构建的。 使用的运行时版本。Net 3.5,我的目标是ARMv7和ARM64,并将其构建为Google App Bundle(*. aab)。 自从上次更新以来,我的游戏崩溃率每天都在增加,90%的崩溃与所有报告的有关设备使用Android 9. 我无法找到我的游戏的哪个部分产生了这个崩溃。我在我的游

  • 问题内容: 如果我的数据是关系型的(出版商-作者-书,协会-团队-玩家),我们可以使用像HBase或MongoDB这样的NoSQL系统来存储数据吗? (我知道这听起来像是一个愚蠢的问题,但我只是在学习:)) 问题答案: 是的,您可以在NoSQL数据存储中存储任何类型的数据。您描述的信息种类对于NoSQL应该是足够的。 但是,请注意,在典型的NoSQL解决方案中,您将交易SQL数据库中某些理所当然的

  • 我试图自动发送短信到某一个号码时,用户按下屏幕上的一个按钮。