【发布时间】:2016-12-11 12:04:44
【问题描述】:
背景
针对 API 24 或更高版本,开发者无需使用简单的“Uri.fromFile”命令,而是需要使用 FileProvider(或自己的 ContentProvider),以便让其他应用访问应用的文件。
问题
我尝试使用以下代码打开一个相机应用程序,让它将文件保存到我的应用程序的私人存储中:
final Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final File photoFile = FileHelper.generateTempCameraFile(this);
mCameraCachedImageURI = FileProvider.getUriForFile(this, getPackageName()+ ".provider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraCachedImageURI);
takePictureIntent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(takePictureIntent, REQ_CODE_PICK_IMAGE_FROM_CAMERA);
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
清单文件
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
这开始很好,因为相机应用程序已启动,但在返回的结果中,我无法使用以前正常工作的代码获取图像文件的方向:
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int orientation = getOrientation(this,mCameraCachedImageURI );
...
public static int getOrientation(final Context context, final Uri photoUri) {
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(photoUri,
new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null);
} catch (final Exception ignored) {
}
if (cursor == null) {
// fallback
return getOrientation(photoUri.getPath());
}
int result = 0;
if (cursor.getCount() > 0) {
cursor.moveToFirst();
result = cursor.getInt(0);
}
cursor.close();
return result;
}
public static int getOrientation(final String filePath) {
if (TextUtils.isEmpty(filePath))
return 0;
try {
final ExifInterface exifInterface = new ExifInterface(filePath);
final int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
return rotate;
} catch (final IOException ignored) {
}
return 0;
}
这会崩溃,因为运行此代码时光标没有任何列。
不仅如此,甚至后备功能(在更改代码以使用它时)也不起作用,因为它在文件路径中添加了一个附加部分(在本例中为“外部”)。
我发现了什么
这里的 contentResolver 似乎使用了 FileProvider,而不是以前版本的 Android 上使用的。
问题是,我需要此 URI 的 ContentProvider 才能授予相机应用访问此文件的权限...
问题
如何使用上面的代码(甚至更好的代码),使用新的 FileProvider Uri 获得方向?也许我可以强制 ContentResolver 使用以前的 ContentProvider 来获取光标所需的列数据?
我真的必须在这里创建自己的 ContentProvider 吗?
【问题讨论】:
-
为什么不使用
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)或Context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)?更多here -
我不想放公共文件,这些文件也可用于图库应用。这是一个临时文件,应该在应用程序本身中使用,很快就会被删除。
-
如果它只是一个私人数据(没有其他应用程序看到它),那么你需要
FileProvider做什么?仅限MediaStore.ACTION_IMAGE_CAPTURE? -
@pskink 它足够私密,不应该让用户轻易找到和查看。我只需要拍摄一张相机图像,然后将其用于某事。
标签: android orientation exif android-fileprovider