我正试图用相机拍照,但我收到以下错误:
FATAL EXCEPTION: main
Process: com.example.marek.myapplication, PID: 6747
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.marek.myapplication/files/Pictures/JPEG_20170228_175633_470124220.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
at com.example.marek.myapplication.MainActivity.dispatchTakePictureIntent(MainActivity.java:56)
at com.example.marek.myapplication.MainActivity.access$100(MainActivity.java:22)
at com.example.marek.myapplication.MainActivity$1.onClick(MainActivity.java:35)
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.marek.myapplication.fileprovider"
android:enabled="true"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
Java的:
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 …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法,使用Android支持库的FileProvider正确地与外部应用程序共享(而不是OPEN)内部文件.
按照文档上的示例,
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.supportv4.my_files"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/my_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
并使用ShareCompat将文件共享给其他应用程序,如下所示:
ShareCompat.IntentBuilder.from(activity)
.setStream(uri) // uri from FileProvider
.setType("text/html")
.getIntent()
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
Run Code Online (Sandbox Code Playgroud)
不起作用,因为FLAG_GRANT_READ_URI_PERMISSION仅授予data对intent 指定的Uri的权限,而不授予EXTRA_STREAMextra 的值(由set设置setStream).
我试图通过设置成安全android:exported到true的供应商,但FileProvider在内部检查,如果本身是出口,所以时,它抛出一个异常.
android android-contentprovider android-support-library android-fileprovider
我的应用程序有一个自动更新功能,下载APK,下载完成后,Intent.VIEW_ACTION打开应用程序,让用户安装下载的apk
Uri uri = Uri.parse("file://" + destination);
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri,
manager.getMimeTypeForDownloadedFile(downloadId));
activity.startActivity(install);
Run Code Online (Sandbox Code Playgroud)
这适用于所有设备<24
现在使用Android 24显然我们不再允许使用file:///启动意图并且在一些谷歌搜索之后建议使用A文件提供程序
新代码:
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri apkUri = FileProvider.getUriForFile(AutoUpdate.this,
BuildConfig.APPLICATION_ID + ".provider", file);
install.setDataAndType(apkUri,
manager.getMimeTypeForDownloadedFile(downloadId));
activity.startActivity(install);
Run Code Online (Sandbox Code Playgroud)
现在activity.startActivity(安装); 抛出错误
找不到处理Intent的活动{act = android.intent.action.VIEW dat = content://com.xxxx.xx.provider/MyFolder/Download/MyApkFile.apk typ = application/vnd.android.package-archive flg = 0x4000000}
有什么方法可以在Android 7(24)中打开APK查看器吗?
android android-intent android-fileprovider viewaction android-7.0-nougat
我有两个应用程序:app1和app2.
App2有:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.android.provider.ImageSharing"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
paths.xml:
<paths>
<files-path name="my_images" path="images/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)
App2从App1的Activity中接收请求以获取图像的URI.确定URI后,App2活动执行以下操作:
Intent intent = new Intent();
intent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
int uid = Binder.getCallingUid();
String callingPackage = getPackageManager().getNameForUid(uid);
getApplicationContext().grantUriPermission(callingPackage, contentUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
setResult(Activity.RESULT_OK, intent);
finish();
Run Code Online (Sandbox Code Playgroud)
从App2收到结果后,App1执行以下操作:
Uri imageUri = data.getData();
if(imageUri != null) {
ImageView iv = (ImageView) layoutView.findViewById(R.id.imageReceived);
iv.setImageURI(imageUri);
}
Run Code Online (Sandbox Code Playgroud)
在App1中,从App2返回时,我得到以下异常:
java.lang.SecurityException:Permission Denial:从ProcessRecord {52a99eb0 3493:com.android.App1.app/u0a57}(pid = 3493,uid = 10057)打开提供者android.support.v4.content.FileProvider uid 10058
我究竟做错了什么 ?
我正在尝试将Camera App存储到我的内部存储器中.我也理解第三方应用程序无法访问我的应用程序的内部存储,但我们可以通过公开内部目录来实现FileProvider.我在这里遵循了指南:
我AndroidManifest.xml用以下方式指定我:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stackoverflow.test.camerawithfileprovider" >
<application>
<activity
...
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.stackoverflow.test.camerawithfileprovider.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_path" />
</provider>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
我创建了一个名为xml文件file_provider_path.xml中/res/xml/:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="image_capture" path="public/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
这就是我创建和调用Camera Intent的方法:
private static final int CAMERA_REQUEST_CODE = 5000;
private static final String CAMERA_FP_AUTHORITY = "com.stackoverflow.test.camerawithfileprovider.fileprovider";
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Context context = MainActivity.this;
File imagePath = new File(context.getFilesDir(), …Run Code Online (Sandbox Code Playgroud) 我正试图用FileProvider私人路径播放视频.面对
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/XXXXX(Package)/files/Videos/final.mp4
Run Code Online (Sandbox Code Playgroud)
码:
<paths>
<files-path path="my_docs" name="Videos/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
Java代码:
File imagePath = new File(getFilesDir(), "Videos");
File newFile = new File(imagePath, "final.mp4");
Log.d(TAG, "-------------newFile:"+newFile.exists());//True here
//Exception in below line
Uri contentUri = FileProvider.getUriForFile(this,"com.wow.fileprovider", newFile);
Run Code Online (Sandbox Code Playgroud)
的Manifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.wow.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
Run Code Online (Sandbox Code Playgroud)
这有什么线索吗?
谢谢Nitz
我在使用时只在我的应用程序中的华为设备上发生异常FileProvider.getUriForFile:
Exception: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/<card name>/Android/data/<app package>/files/.export/2016-10-06 13-22-33.pdf
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(SourceFile:711)
at android.support.v4.content.FileProvider.getUriForFile(SourceFile:400)
Run Code Online (Sandbox Code Playgroud)
以下是我的清单中文件提供程序的定义:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
具有已配置路径的资源文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="external_files" path="" />
</paths>
Run Code Online (Sandbox Code Playgroud)
关于这个问题的原因以及它为什么只在华为设备上发生的任何想法?鉴于我没有华为设备,我该怎么调试呢?
更新:
我已经添加了更多的登录到我的应用程序,我得到了一些打印时都不一致的结果ContextCompat.getExternalFilesDirs,并context.getExternalFilesDir在这些设备:
ContextCompat.getExternalFilesDirs:
/storage/emulated/0/Android/data/<package>/files
/storage/sdcard1/Android/data/<package>/files
context.getExternalFilesDir:
/storage/sdcard1/Android/data/<package>/files
Run Code Online (Sandbox Code Playgroud)
这与该ContextCompat.getExternalFilesDirs州的文件不一致The first path returned is the same as getExternalFilesDir(String)
这解释了我context.getExternalFilesDir在我的代码和FileProvider使用中使用的问题ContextCompat.getExternalFilesDirs.
我正在尝试与 FileProvider 共享文件。我检查了该文件是否与 gmail、google drive 等应用程序正确共享。即使抛出以下异常:
2019-08-28 11:43:03.169 12573-12595/com.example.name E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.example.name.provider/external_files/Android/data/com.example.name/files/allergy_report.pdf from pid=6005, uid=1000 requires the provider be exported, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:729)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:602)
at android.content.ContentProvider$Transport.query(ContentProvider.java:231)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:104)
at android.os.Binder.execTransactInternal(Binder.java:1021)
at android.os.Binder.execTransact(Binder.java:994)
Run Code Online (Sandbox Code Playgroud)
提供者:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
file_provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>
Run Code Online (Sandbox Code Playgroud)
分享意向
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
File fileWithinMyDir = new File(targetPdf);
if(fileWithinMyDir.exists()) {
intentShareFile.setType("application/pdf"); …Run Code Online (Sandbox Code Playgroud) android android-intent android-permissions android-fileprovider
我在FileProviders上的android开发人员参考上遵循了示例代码,但它不起作用.
我files/exports/在我的Manifest中的文件提供程序定义中设置了路径,并且引用的文件确实存在于该路径中.
在我的清单中:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.company.app.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
并在 xml/file_paths.xml
<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="exports" path="exports/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用以下代码获取content-uri:
Java.IO.File exportFile =
new Java.IO.File ("/data/data/com.company.app/files/exports/file.pdf");
Android.Net.Uri exportUri =
Android.Support.V4.Content.FileProvider.GetUriForFile (this, "com.company.app.fileprovider", exportFile);
Run Code Online (Sandbox Code Playgroud)
但是我收到此错误:
Exception of type 'Java.Lang.NullPointerException' was thrown.
at Android.Runtime.JNIEnv.CallStaticObjectMethod (IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00064] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/4b53fbd0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1160
at Android.Support.V4.Content.FileProvider.GetUriForFile (Android.Content.Context context, System.String authority, Java.IO.File file) [0x00000] in <filename unknown>:0
at com.company.App.ImageAndSubtitle.cloudStorageDidDownloadFile (System.Object sender, com.company.App.CloudStorageEventArgs …Run Code Online (Sandbox Code Playgroud) 我正在尝试升级可运行的旧应用程序以支持Android API 26,而我需要使用的功能之一是android.support.v4.content.FileProvider-但未找到。
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
由于早期的Android构建,gradle文件似乎很简单。是否像添加依赖项一样简单?我环顾四周,有些人建议添加一个我不了解的multidex。任何帮助表示赞赏,谢谢!
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "virtualgs.spaint"
minSdkVersion 22
targetSdkVersion 26
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
Run Code Online (Sandbox Code Playgroud)