Android Fileprovider 无法获取 uri
Android Fileprovider unable to get uri
我使用 Fileprovider 获取我手动保存在设备 "F1" 文件夹中的图像的 Uri Storage.When 我试图获取 Uri,但出现 NullPointerException。异常是在我尝试访问 Uri 的地方。我在 Manifest 中声明了提供者并在 xml 中声明了路径 file.Have 我在 xml?
中设置了错误的路径
代码:
folder=new File(Environment.getExternalStorageDirectory()+"/F1");
folder.mkdirs();
File file1=new File(folder+"/37deb43.jpg");
Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",file1);
Manifest.xml:
<provider
android:authorities="${applicationId}"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/share_file_paths"
>
</meta-data>
</provider>
@xml/share_file_paths:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="external_files"
path="F1">
</external-files-path>
</paths>
异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:604)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:578)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:416)
at com.globemaster.com.architecturecomponentstest.mvvm.java_technical_programs.JavaTechnicalActivity.fetchuri(JavaTechnicalActivity.java:179)
at com.globemaster.com.architecturecomponentstest.mvvm.java_technical_programs.JavaTechnicalActivity.access0(JavaTechnicalActivity.java:30)
at com.globemaster.com.architecturecomponentstest.mvvm.java_technical_programs.JavaTechnicalActivity.onClick(JavaTechnicalActivity.java:130)
at android.view.View.performClick(View.java:5716)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我猜您的第一个代码段中存在复制粘贴错误!试试这个:
File folder=new File(Environment.getExternalStorageDirectory(), "F1");
folder.mkdirs();
File file1=new File(folder, "37deb43.jpg");
Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",file1);
Log.d("URI", "Uri is: " + uri)
我终于得到 it.The 问题出在 getUriForFile()
的第二个参数中。
Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",file1);
我将 "provider" 附加到 "BuildConfig.APPLICATION_ID"
below.It 不是 needed.The 下面的代码工作正常 now.But 谢谢大家努力帮助我
..
Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID,file1);
我使用 Fileprovider 获取我手动保存在设备 "F1" 文件夹中的图像的 Uri Storage.When 我试图获取 Uri,但出现 NullPointerException。异常是在我尝试访问 Uri 的地方。我在 Manifest 中声明了提供者并在 xml 中声明了路径 file.Have 我在 xml?
中设置了错误的路径代码:
folder=new File(Environment.getExternalStorageDirectory()+"/F1");
folder.mkdirs();
File file1=new File(folder+"/37deb43.jpg");
Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",file1);
Manifest.xml:
<provider
android:authorities="${applicationId}"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/share_file_paths"
>
</meta-data>
</provider>
@xml/share_file_paths:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="external_files"
path="F1">
</external-files-path>
</paths>
异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:604)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:578)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:416)
at com.globemaster.com.architecturecomponentstest.mvvm.java_technical_programs.JavaTechnicalActivity.fetchuri(JavaTechnicalActivity.java:179)
at com.globemaster.com.architecturecomponentstest.mvvm.java_technical_programs.JavaTechnicalActivity.access0(JavaTechnicalActivity.java:30)
at com.globemaster.com.architecturecomponentstest.mvvm.java_technical_programs.JavaTechnicalActivity.onClick(JavaTechnicalActivity.java:130)
at android.view.View.performClick(View.java:5716)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
我猜您的第一个代码段中存在复制粘贴错误!试试这个:
File folder=new File(Environment.getExternalStorageDirectory(), "F1");
folder.mkdirs();
File file1=new File(folder, "37deb43.jpg");
Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",file1);
Log.d("URI", "Uri is: " + uri)
我终于得到 it.The 问题出在 getUriForFile()
的第二个参数中。
Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",file1);
我将 "provider" 附加到 "BuildConfig.APPLICATION_ID"
below.It 不是 needed.The 下面的代码工作正常 now.But 谢谢大家努力帮助我
..
Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID,file1);