我无法理解我将更改哪些代码以设置自定义文件存储路径。此活动成功启动了Android相机应用程序,让您拍摄视频,然后返回到视频保存到的活动:“文件:///storage/emulated/0/videoapp/examplevideoname.mp4”
我只是想将它保存到我选择的其他文件夹中,但这是来自Google教程,我无法弄清楚我需要更改哪个部分: http://developer.android.com/guide/topics/media/camera.html#intents http://developer.android.com/guide/topics/data/data-storage.html#db
所有帮助都非常感谢!谷歌代码甚至被评论为有帮助,但我已经足够新,我仍然不清楚。
对我来说很明显“getExternalStoragePublicDirectory”与它有关,但我无法弄清楚我是否应该用我的新路径或其他东西替换那部分。
以下是代码段:
的onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
// start the Video Capture Intent
startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Video captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Video saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
} else {
// Video capture failed, advise user
}
}
}
getOutputMediaFile:
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), "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_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
答案 0 :(得分:1)
如果你想创建自己的文件夹,那么你必须在这里更改
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
其中,“MyCameraApp”是您的文件夹名称,因此您可以替换您的文件夹和Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)这是您的目录,如果您只想保存SD卡,那么请用Environment.getExternalStorageDirectory替换。它在/sdcard/your_folder_name/your_video_file.mp4中创建一个文件夹