我正试图用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
我有一个关于android FileProvider的问题.我想保存一个pdf文档并使用默认程序打开它.我不想将它保存在外部存储中.
在我成功将pdf保存到FilesDirectory/export/temp.pdf之后,
我尝试使用FileProvider.getUriForFile()生成URI.
File path = new File(getFilesDir(), "export");
File pdf = new File(path + File.separator + "temp.pdf");
pdf.getParentFile().mkdirs();
if (!pdf.exists())
pdf.createNewFile();
Uri uri = FileProvider.getUriForFile(getApplicationContext(), "?", pdf);
Run Code Online (Sandbox Code Playgroud)
问题:我必须传递什么作为第二个参数"权限" - 我的文件的位置,可以授予URI权限的类或其他什么?无论我尝试过什么导致IllegalArgumentException或NullPointerException.我的FileProvider(XML):
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.myApp.myActivity"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path"/>
</provider>
Run Code Online (Sandbox Code Playgroud)
引用文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="pdfTemplates" path="export/" />
</paths>
Run Code Online (Sandbox Code Playgroud) 我正在使用文件提供程序将照片保存到给定目的地.我明白了:
java.lang.IllegalArgumentException:尝试打开活动以从摄像头捕获图像时缺少android.support.FILE_PROVIDER_PATHS元数据.
我的manifest.xml文件:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example"
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 xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="content" path="Android/data/com.my_package_name/files/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
和Java代码:
File externalFilesDirectory = this.getExternalFilesDir(null);
File imageFile = File.createTempFile(
imageFileName,
".jpg",
externalFilesDirectory
);
Uri photoURI = FileProvider.getUriForFile(this, "com.example", imageFile);
Run Code Online (Sandbox Code Playgroud)
最后一行给出了例外.我在这里错过了什么?我已经关注了官方Android开发网站的教程(https://developer.android.com/training/camera/photobasics.html)
android android-image android-xml android-file android-fileprovider
我的清单中有以下FileProvider:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.android.provider.DataSharing-1"
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)
我在应用启动时遇到以下异常:
java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.IllegalArgumentException: Name must not be empty
at android.app.ActivityThread.installProvider(ActivityThread.java:4793)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4385)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4325)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Name must not be empty
at android.support.v4.content.FileProvider$SimplePathStrategy.addRoot(FileProvider.java:644)
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:587)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
at android.support.v4.content.FileProvider.attachInfo(FileProvider.java:352)
at android.app.ActivityThread.installProvider(ActivityThread.java:4790) ...
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我在哪里错过了一个名字?
编辑:
RES/XML/paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path android:name="my_images" android:path="images/"/>
</paths>
Run Code Online (Sandbox Code Playgroud) 我看了很多类似的帖子,但仍然不知道问题出在哪里。我尝试更改 file_paths.xml 中的路径和名称。
文件路径.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="pirosfogo_images"
path="storage/emulated/0/pictures/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml:
<application
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.kijevigombooc.pirosfogo.fileprovider"
android:exported="false"
android:grantUriPermissions="true"/>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</application>
Run Code Online (Sandbox Code Playgroud)
爪哇:
void takePhoto(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 110);
}
else
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null)
{
File photoFile = createPhotoFile();
if(photoFile != null){
pathToFile = photoFile.getAbsolutePath();
Uri photoURI = FileProvider.getUriForFile(ProfileEdit.this, "adada", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_CAMERA);
}
}
}
}
private File createPhotoFile() { …Run Code Online (Sandbox Code Playgroud)