【问题标题】:Android - Video can't be savedAndroid - 无法保存视频
【发布时间】:2015-12-06 20:42:22
【问题描述】:

我们正在尝试制作一个可以将视频保存到目录的应用。但是,它不会保存为 mp4 文件,而是保存为 FOLDER!例如,当我录制视频时,它会保存为文件夹(/root/DCIM/sdcard/DCIM/Camera App/video.mp4/)。

下面是我们的代码:

/** * 对于视频 */ //上下文上下文= this;

private boolean prepareMediaRecorder() {
    String path = String.valueOf(getOutputMediaFile(MEDIA_TYPE_VIDEO));
    File file = new File(path, "Eye Spy");
    file.mkdirs();
    mMediaRecorder = new MediaRecorder();


    try {
        mCamera.unlock();
    } catch (Exception ex) {
        return false;
    }


    // adjust the camera the way you need
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

    //
    CamcorderProfile profile = CamcorderProfile.get(Camera.CameraInfo.CAMERA_FACING_BACK, CamcorderProfile.QUALITY_LOW);
    profile.fileFormat = MediaRecorder.OutputFormat.MPEG_4;
    profile.videoCodec = MediaRecorder.VideoEncoder.MPEG_4_SP;
    profile.videoFrameHeight = 240;
    profile.videoFrameWidth = 320;
    profile.videoBitRate = 15;
    mMediaRecorder.setProfile(profile);
    mMediaRecorder.setPreviewDisplay(mPreview.getSurface());
    mMediaRecorder.setOutputFile(path);

    //set max size
    mMediaRecorder.setMaxDuration(600000); // Set max duration 60 sec.
    mMediaRecorder.setMaxFileSize(50000000); // Set max file size 50M

    try {
        mMediaRecorder.prepare();
    } catch (Exception e) {
        releaseMediaRecorder();
        e.printStackTrace();
    }
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    getBaseContext().sendBroadcast(mediaScanIntent);

    return true;
}

private void releaseMediaRecorder() {
    if (mMediaRecorder != null) {
        mMediaRecorder.reset(); // clear recorder configuration
        mMediaRecorder.release(); // release the recorder object
        mMediaRecorder = null;
        if (mCamera != null) {
            mCamera.lock(); // lock camera for later use
        }
    }
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

        if (pictureFile == null) {
            Log.i("TRY:", " Error creating media file, check storage permissions:");
            Log.d(TAG, "Error creating media file, check storage permissions: " +
                    e.getMessage());
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            Toast.makeText(InstagramProfilePage.this, "Picture taken!", Toast.LENGTH_SHORT).show();
            /*loc2Exif(pictureFile);*/
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri contentUri = Uri.fromFile(pictureFile);
        mediaScanIntent.setData(contentUri);
        getBaseContext().sendBroadcast(mediaScanIntent);
        mCamera.startPreview();

    }




    /*mCamera.startPreview();*/
};

/**
 * 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) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "Eye Spy");

    // 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("Eye Spy", "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() + File.separator +
                "IMG_" + timeStamp + ".jpg");
        Log.i("TRY:", " PICTURE SAVED");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "Eye Spy");
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_" + timeStamp + ".mp4");
    } else {
        Log.i("TRY:", " file = null");
        return null;
    }
    Log.i("TRY:", mediaFile.getPath());
    return mediaFile;
}
 private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);

        if (pictureFile == null) {
            Log.i("TRY:", " Error creating media file, check storage permissions:");
            Log.d(TAG, "Error creating media file, check storage permissions: " +
                    e.getMessage());
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            Toast.makeText(InstagramProfilePage.this, "Picture taken!", Toast.LENGTH_SHORT).show();
            /*loc2Exif(pictureFile);*/
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri contentUri = Uri.fromFile(pictureFile);
        mediaScanIntent.setData(contentUri);
        getBaseContext().sendBroadcast(mediaScanIntent);
        mCamera.startPreview();

    }




    /*mCamera.startPreview();*/
};

/**
 * 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) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "Eye Spy");

    // 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("Eye Spy", "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() + File.separator +
                "IMG_" + timeStamp + ".jpg");
        Log.i("TRY:", " PICTURE SAVED");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "Eye Spy");
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_" + timeStamp + ".mp4");
    } else {
        Log.i("TRY:", " file = null");
        return null;
    }
    Log.i("TRY:", mediaFile.getPath());
    return mediaFile;
}

【问题讨论】:

    标签: java android video


    【解决方案1】:

    当您在第二行代码中调用 getOutputMediaFile(MEDIA_TYPE_VIDEO) 时,您将获得 .mp4 File 并且当您将其转换为字符串时:

    String path = String.valueOf(getOutputMediaFile(MEDIA_TYPE_VIDEO));
    

    你得到文件的绝对路径,然后你调用file.mkdirs(),它从该路径创建一个文件夹,基本上你是说创建一个文件夹而不是文件。您要做的是创建文件夹,然后创建实际文件。

    解决方案

    而不是调用:

    file.mkdirs();
    

    使用这行代码创建文件父文件夹:

    file.getParentFile().mkdirs();
    

    【讨论】:

    • 是的!谢谢@Arlind!但是,它保存了损坏的文件?这里似乎有什么问题?
    • @user5290675 你可能想为此写一个不同的问题。但我会检查的。
    猜你喜欢
    • 2019-05-27
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    相关资源
    最近更新 更多