Android官方文档中给出的教程对于Android10(API29)有一些问题,还有我自己的一些理解问题,导致踩了无数坑,在这里记录一下:
1. 利用时间戳创建独一无二的文件名:
private File createImageFile() throws IOException {
// 创建一个图像文件名
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
注意:
(1) 在自己定义文件前缀名时,如果带有斜杠/,则会报错。例:
正确:
File directory = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
final String IMAGE_PREFIX = "image_xxxx";
当自己指定外部存储路径子目录名称时:
File directory = getExternalFilesDir("test/");
final String IMAGE_PREFIX = "image_xxxx";
错误:
File directory = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
final String IMAGE_PREFIX = "/image_xxxx";
当自己指定外部存储路径子目录名称时:
File directory = getExternalFilesDir("test");
final String IMAGE_PREFIX = "/image_xxxx"; // 文件前缀名中不能含有斜杠"/"!!!
异常信息:java.io.IOException: Unable to create temporary file
(2) 在创建文件时抛出异常 java.io.IOException: No such file or directory
这是由于如果directory不存在,则createTempFile无法找到directory的路径,需要先通过mkdirs创建文件夹,再调用createTempFile创建文件
File directory = new File("D:\\Test\\");
try {
// 先检查目录是否存在,如果不存在则创建目录
if (!directory.exists()) {
directory.mkdirs();
}
File file = File.createTempFile("test", ".txt", directory);
} catch (IOException e) {
e.printStackTrace();
}
2. 配置应用清单
<application>
...
<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>
...
</application>
确保android:authorities="com.example.android.fileprovider"与getUriForFile(Context, String, File)中的String参数一致。
3. 配置file_paths.xml,与android:resource="@xml/file_paths"对应
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Android/data/com.example.package.name/files/Pictures" />
</paths>
注意,在官方文档给出的教程之外,还应该在file_paths.xml中添加根目录路径,不添加可能会产生异常:Failed to find configured root that contains
<root-path name="root_path" path="."/>
完整的file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_sources" path="Android/data/edu.bupt.ticketextraction/files"/>
<root-path name="root_path" path="."/>
</paths>
4. 调用相机:
static final int REQUEST_TAKE_PHOTO = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
if (photoFile != null) {
// "com.example.android.fileprovider"应与在应用清单中的authorities一致
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
直接调用相机会引发java.lang.SecurityException: Permission Denial with revoked permission android.permission.CAMERA的异常,必须先获得授权再调用相机功能。
正确代码:
private static final int START_CAMERA = 1;
public void takePicture() {
int checkPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (checkPermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, START_CAMERA);
} else {
startCamera();
}
}
private void startCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
File imageFile = curWallet.createImage();
if (imageFile != null) {
Uri uri = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
imageFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, START_CAMERA);
}
}
}
一杯茶,一包烟,一个bug修一天。