内置相机应用程序正确保存我的视频后出现神秘的 NullpointerException

发布于 2024-12-11 13:12:09 字数 2075 浏览 0 评论 0原文

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

这是日志猫:

在此处输入图像描述

代码:

这些来自我调用相机应用程序的对话框

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

此代码更多或更少从 android 开发者网站复制。 正如我所提到的,在此之后,甚至我的活动的 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.

Here is the Log Cat:

enter image description here

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩动你心 2024-12-18 13:12:09

试试这个:

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 方法返回一个 File 对象,而不是一个可以附加子目录的字符串。

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

在具有多个用户的设备上(如 UserManager 所描述),每个用户
用户有自己独立的外部存储。应用程序只有
访问其运行用户的外部存储。

哈哈!!!!才发现这个问题是2年前问的!你找到答案了吗??哈哈

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文