我正在尝试将一些旧应用从file://更改为content:// URIs,我的结果好坏参与。
我在另一个StackOverflow问题中找到了此链接:https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en并按照教程进行了操作。
这是我用来创建文件并在调用摄像头之前获取URI的代码
photoFile = null;
try {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"qmobility");
photoFile = File.createTempFile(imageFileName, ".jpg", storageDir);
mCurrentPhotoPath = photoFile.getAbsolutePath();
} catch (Exception e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
if (photoFile != null) {
String authority = this.getApplicationContext().getPackageName() + ".qphoto.GenericFileProvider";
photoURI = GenericFileProvider.getUriForFile(this,
authority,
photoFile);
}
我在清单
中定义了此提供程序 <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="device.qmobility.quiter.com.qmobilitydeviceapp.qphoto.GenericFileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
file_paths文件包含此
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="/Pictures/"/>
</paths>
应用程序应该将图片存储在此路径中:/storage/emulated/0/Pictures/qmobility/
我从主要活动中获取此路径。
但在其他活动中,我得到以下值:
storageDir:/storage/emulated/0/Pictures/qmobility
photoFile:/storage/emulated/0/Pictures/qmobility/JPEG_20180308_133202_-1027794396.jpg
权威:device.qmobility.quiter.com.qmobilitydeviceapp.qphoto.GenericFileProvider
photoUri:content://device.qmobility.quiter.com.qmobilitydeviceapp.qphoto.GenericFileProvider/external_files/qmobility/JPEG_20180308_133255_-1768273985.jpg
如何获取content:///storage/emulated/0/Pictures/qmobility/JPEG_20180308_133255_-1768273985.jpg
????