如何从相机意图中捕获视频?

时间:2017-09-29 13:40:09

标签: android android-camera video-capture

我想从相机Intent捕获视频并保存在app目录中(不要在设备库中保存视频)并将视频路径插入数据库以加载并显示在我的应用中

我尝试了我的活动:

    public class VideoActivity extends Activity {
    private TblVideo TBL_VIDEO;
    private Uri fileUri;
    public static final int MEDIA_TYPE_VIDEO = 2;
    private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

    long date = System.currentTimeMillis();
    String string_path = DATABASE_LOCATION + LAST_MOMENT_ID + "_" + date + ".mp4";
    File mediaFile = new File(string_path);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        Button buttonRecording = (Button) findViewById(R.id.photo_btn_take_video);
        buttonRecording.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                try {
                    ContentValues values = new ContentValues();
                    fileUri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
                    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                    fileUri = Uri.fromFile(mediaFile);
                    startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
                    getContentResolver().delete(fileUri, null, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private String SaveMediaFile(int type) {
        if (type == MEDIA_TYPE_VIDEO) {
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(mediaFile);
            } catch (Exception e) {
                displayToast(this, "خطای ذخیره ویدیو:" + "\n" + e.toString());
            } finally {
                try {
                    assert fileOutputStream != null;
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return string_path;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
            try {
                if (resultCode == RESULT_OK) {
                    insertVideo();
                } else if (resultCode == RESULT_CANCELED) {
                    displayToast(this, "ضبط لغو شد");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

但是当转到文件目录时,视频大小为0Kb !!并保存到设备库中 如何解决这个问题? 感谢

2 个答案:

答案 0 :(得分:1)

然后尝试这个,但应用程序崩溃了:

buttonRecording.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            try {
                ContentValues values = new ContentValues();
                fileUri = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
                Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            fileUri = data.getData();
            FileOutputStream fileOutputStream = null;
            try {
                FileInputStream fileInputStream = new FileInputStream(fileUri.getPath());
                fileOutputStream = new FileOutputStream(mediaFile);
                byte[] buf = new byte[1024];
                int len;
                while ((len = fileInputStream.read(buf)) > 0) {
                    fileOutputStream.write(buf, 0, len);
                }
                fileInputStream.close();
                fileOutputStream.close();
                insertVideo();
                getContentResolver().delete(fileUri, null, null);
            } catch (Exception e) {
                displayToast(this, "خطای ذخیره ویدیو:" + "\n" + e.toString());
            } finally {
                try {
                    assert fileOutputStream != null;
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else if (resultCode == RESULT_CANCELED) {
            displayToast(this, "ضبط لغو شد");
        }
    }
}

答案 1 :(得分:1)

我使用此功能并在活动结果正常后保存视频文件,将视频保存在目录中!

private String SaveMediaFile() {
    try {
        InputStream in = getContentResolver().openInputStream(fileUri); // Uri
        OutputStream out = new FileOutputStream(mediaFile); // file
        byte[] buf = new byte[1024];
        int len;
        assert in != null;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();
        in.close();
    } catch (Exception e) {
        displayToast(this, "خطای ذخیره ویدیو");
    }
    return string_path;
}

感谢