内置相机应用程序后,神秘NullpointerException会正常保存我的视频 [英] Mysterious NullpointerException after the built-in camera app saves my video properly

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

问题描述

我有一个活动,如果您打开对话框并点击图标,您可以录制视频。
问题是,在我停止录制后,它会抛出一个NullPointerException异常,即使视频被正确保存。根据Log Cat的错误不是在我的代码,所以我试图在我的代码中放置检查点,我发现,即使我的活动的onActivityResult正确执行,现在我不知道该做什么。 p>

这里是日志猫:





代码:



调用相机应用程序的对话框

  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); //将视频图像质量设置为高
//启动视频捕获Intent

((Activity)上下文).startActivityForResult(intent,CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);



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


私人静态文件getOutputMediaFile(int type)
{
//为了安全起见,你应该检查SD卡是否已装载
//在此之前使用Environment.getExternalStorageState()。
文件mediaStorageDir =新文件(Environment.getExternalStorageDirectory()+/ Movies,MyCameraApp);
//如果您希望在应用程序之间共享创建的映像
//,则此位置最适合,并且在您的应用程序卸载后仍然保留。

//如果不存在则创建存储目录
if(!mediaStorageDir.exists()){
if(!mediaStorageDir.mkdirs()){
Log.d(MyCameraApp,无法创建目录);
return null;
}
}

//创建一个媒体文件名
String timeStamp = new SimpleDateFormat(yyyyMMdd_HHmmss)format(new Date());
文件mediaFile;
if(type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath()+/+
IMG _+ timeStamp +。
} else if(type == MEDIA_TYPE_VIDEO){
mediaFile = new File(mediaStorageDir.getPath()+/+
VID _+ timeStamp +.mp4);
} else {
return null;
}

return mediaFile;
}

这段代码或多或少地从android开发者网站复制。
正如我所提到的,甚至我的活动的onActivityResult被正确执行(我在这之后关闭对话框)。

解决方案

尝试:

 私人静态文件getOutputMediaFile(int type)
{
文件mediaStorageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);

if(!mediaStorageDir.exists()){
if(!mediaStorageDir.mkdirs()){
Log.d(MyCameraApp,无法创建目录 );
return null;
}
}

//创建一个媒体文件名
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方法返回一个File对象,而不是一个字符串,至。



它也想知道该方法返回的目录是否可用于服务。 Android规范说:


在具有多个用户的设备上(如UserManager所述),每个
用户都有自己的隔离外部存储。应用程序只有
的访问外部存储为他们运行的用户。


LOL !!!!只是意识到这个问题是在2年前问的!你找到答案了吗? LOL


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.

Here is the Log Cat:

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;
}

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.

解决方案

Try 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);
}

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

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

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!!!! Just realized this question was asked 2 years ago! Did you find the answer yet?? LOL

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

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