内置的摄像头应用程序后,神秘的NullPointerException妥善保存我的视频 [英] Mysterious NullpointerException after the built-in camera app saves my video properly

查看:262
本文介绍了内置的摄像头应用程序后,神秘的NullPointerException妥善保存我的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,可以让你,如果你打开​​一个对话框,点击一个图标录制视频。
问题是,我停止录音后,它抛出,即使视频是正确保存NullPointerException异常。根据日志猫错误是不是在我的code所以我试图把闯关在我的code和我发现,我的活动,甚至是的onActivityResult所以现在我出的主意的正确执行该怎么做。

I have an activity that allows you to record a video if you open a dialog and click on an icon. The problem is that after I stop recording it throws a NullPointerException even though the video is saved properly. According to Log Cat the error is not in my code so I tried to place "checkpoints" in my code and I found out that even the onActivityResult of my activity is executed properly so now I'm out of idea what to do.

下面是日志猫:

code:

这些都是从我的对话框,调用相机应用

these are from my dialog that invokes the camera app

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); 


intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
// start the Video Capture Intent

((Activity)context).startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);



private static Uri getOutputMediaFileUri(int type)
{ 
    return Uri.fromFile(getOutputMediaFile(type));
}


private static File getOutputMediaFile(int type)
{
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
  File mediaStorageDir = new File(Environment.getExternalStorageDirectory()+"/Movies",   "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // 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_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + "/" +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + "/" +
        "VID_"+ timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

这code或多或少从Android开发者网站复制。
正如我提到我的活动连的onActivityResult正确执行(其中我关闭该对话框),在此之后。

This code was more or less copied from the android developers site. As I mentioned even the onActivityResult of my activity is executed properly(where I dismiss the dialog) after this.

推荐答案

试试这个:

private static File getOutputMediaFile(int type)
{
  File mediaStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);

    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());
    String mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = "IMG_"+ timeStamp + ".jpg";
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = "VID_"+ timeStamp + ".mp4";
    } else {
        return null;
    }

    return new File(mediaStorageDir, mediaFile);
}

该getExternalStorageDirectory方法返回一个文件对象,而不是可以追加一个子目录的字符串。

The getExternalStorageDirectory method returns a File object, not a string you can append a subdirectory to.

这也怀疑是否由该方法返回的目录将是可用的服务。 Android的规格说:

It also wonder if the directory returned by that method would be available to a service. The Android specs say:

在与多个用户的设备(如通过的UserManager描述),各
  用户拥有它们自己独立的外部存储。应用程序只能
  获得对他们正在运行的用户外部存储。

On devices with multiple users (as described by UserManager), each user has their own isolated external storage. Applications only have access to the external storage for the user they're running as.

LOL !!!!刚刚意识到这有人问2年前!你找到答案吗?? LOL

LOL!!!! Just realized this question was asked 2 years ago! Did you find the answer yet?? LOL

这篇关于内置的摄像头应用程序后,神秘的NullPointerException妥善保存我的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆