我使用Fileprovider获取手动保存在设备存储中“ F1”文件夹中的图像的Uri。当我尝试获取Uri时,我获取了NullPointerException。我尝试访问Uri的地方是例外。我已经在清单中声明了provider并且在xml文件中声明了路径。是否在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.access$200(JavaTechnicalActivity.java:30)
at com.globemaster.com.architecturecomponentstest.mvvm.java_technical_programs.JavaTechnicalActivity$4.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)
答案 0 :(得分:1)
我终于明白了。问题出在getUriForFile()
的第二个参数中。
Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",file1);
我在下面的"BuildConfig.APPLICATION_ID"
后面加上了“ provider”。现在不需要了。下面的代码现在可以正常工作。但是,谢谢大家为帮助我所做的努力
..
Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID,file1);
答案 1 :(得分:0)
我猜您在第一个代码段中存在复制粘贴错误!试试这个:
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)