我想捕捉视频并将视频存储在默认位置以外的特定位置。
我知道有一个名为setOutPutFile("String Location")
的MediaStore方法。
但它运作不正常。
我已经看过许多示例并已执行,但它仅将视频存储在默认位置。有人可以帮我解决这个问题吗?
答案 0 :(得分:13)
这样做:
全球宣布
public static final int TAKE_PICTURE=0;
然后
Intent photoPickerIntent= new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(Intent.createChooser(photoPickerIntent,"Take Video"),TAKE_VIDEO);
在OnActivityResult中以这种方式处理:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if(requestCode==TAKE_VIDEO)
{
try
{
Log.e("videopath","videopath");
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
FileInputStream fis = videoAsset.createInputStream();
File root=new File(Environment.getExternalStorageDirectory(),"Directory Name");
if (!root.exists()) {
root.mkdirs();
}
File file;
file=new File(root,"android_"+System.currentTimeMillis()+".mp4" );
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
使用以下权限:
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
答案 1 :(得分:2)
我对此处提供的任何解决方案都不满意。这是一个非常简单的解决方案,可以将视频保存在您选择的文件夹中。
private static final int REQUEST_VIDEO_CAPTURE = 300;
private Uri videoUri;
private void dispatchVideoIntent(){
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
File outputFile = new File("your file path goes here");
videoUri = Uri.fromFile(outputFile);
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK && videoUri != null) {
// do what you want with videoUri
}
}
答案 2 :(得分:1)
abhi给出的答案是正确的,但我正在写一个新的答案,让其他用户更容易和简单。
要录制视频,请在onCreate
方法或按钮上单击要开始录制视频的位置编写此代码
private static final int ACTION_TAKE_VIDEO = 3;
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(takeVideoIntent, ACTION_TAKE_VIDEO);
此代码将开始录制视频。
现在,在onCreate
方法之后的代码中,您需要覆盖名为onActivityResult
的方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK)
{
try
{
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
FileInputStream fis = videoAsset.createInputStream();
File root=new File(Environment.getExternalStorageDirectory(),"/RecordVideo/"); //you can replace RecordVideo by the specific folder where you want to save the video
if (!root.exists()) {
System.out.println("No directory");
root.mkdirs();
}
File file;
file=new File(root,"android_"+System.currentTimeMillis()+".mp4" );
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
将“RecordVideo”替换为SD卡上视频文件所需的特定文件夹。您也可以在此处使用任何现有文件夹或目录名称。
由于