我目前正在尝试创建一个按钮来打开相机应用程序,并使用特定的名称和位置保存图片。
这用于创建名称/目录
// create a filename for image to be stored into
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String Calib_image_File_Name = "Calibration" + "_" + timeStamp + "STOP";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
Calib_image_File_Name, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
这是我的处理程序
public void Take_Image_calib(View view)
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
// ...
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
我遇到的问题是,在“停止”之后,它会在之后添加一个19位数字。我不知道为什么会这样,建议?
编辑:
文件提供商
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
答案 0 :(得分:0)
这种情况正在发生,因为其定义中的createTempFile
有这一行:
...new File(tmpDirFile, prefix + tempFileRandom.nextInt() + suffix);
我相信会导致问题。只需使用new File(...)
:
File image = new File(storageDir, Calib_image_File_Name + ".jpg");