【发布时间】:2012-01-21 03:00:17
【问题描述】:
使用默认相机应用的照片和视频保存在 SD 卡上。
我真的需要知道是否有办法(简单或困难)更改路径以便我可以将这些文件保存在内存中。
或者,如果您知道 android 市场上的另一个相机应用程序可以选择更改路径。
我不需要 SD 卡解决方案。
【问题讨论】:
标签: java android mobile android-camera
使用默认相机应用的照片和视频保存在 SD 卡上。
我真的需要知道是否有办法(简单或困难)更改路径以便我可以将这些文件保存在内存中。
或者,如果您知道 android 市场上的另一个相机应用程序可以选择更改路径。
我不需要 SD 卡解决方案。
【问题讨论】:
标签: java android mobile android-camera
你可以这样做,
这适用于我的情况..
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
startActivityForResult(intent, TAKE_PHOTO_CODE);
还有getImageUri()
/**
* Get the uri of the captured file
* @return A Uri which path is the path of an image file, stored on the dcim folder
*/
private Uri getImageUri() {
// Store image in dcim
// Here you can change yourinternal storage path to store those images..
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM", CAPTURE_TITLE);
Uri imgUri = Uri.fromFile(file);
return imgUri;
}
更多信息请查看How to capture an image and store it with the native Android Camera
编辑:
在我的代码中,我将图像存储在SDCARD,但您可以根据需要提供内部存储路径,例如/data/data/<package_name>/files/..
您可以使用Context.getFilesDir()。但请记住,即使默认情况下它也是您的应用程序私有的,因此其他应用程序(包括媒体商店)将无法访问它。也就是说,您始终可以选择将文件设置为可读或可写。
还有Context.getDir() 和Context.MODE_WORLD_WRITEABLE 可以写入其他应用程序可以写入的目录。但同样,我质疑将图像数据存储在本地存储中的必要性。用户不会意识到这一点,除非用户在使用您的应用时不会安装 SD 卡(这并不常见)。
【讨论】:
是的,我相信可以从开发人员指南中查看此内容。
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
/** 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 Uri 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_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
【讨论】: