Android 7.0调用相机出错

gys19921114 2016-12-16 06:13:29
我在写android头像上传,发现7.0的手机在调用时报错。代码是网上找的。
错误信息:

Process: com.example.administrator.myapplication, PID: 3997
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:583)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:399)

Manifest里的provider:

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.administrator.myapplication.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>

调用代码:

File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
if (!file.getParentFile().exists())file.getParentFile().mkdirs();
//这行报错
Uri imageUri = FileProvider.getUriForFile(getActivity(), "com.example.administrator.myapplication", file);
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将拍取的照片保存到指定URI
startActivityForResult(intent,1006);

file_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="" name="camera_photos" />
</paths>

求各位大神指教!
...全文
775 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bodetownpas 2017-07-28
  • 打赏
  • 举报
回复 1
将 <provider android:name="android.support.v4.content.FileProvider" android:authorities="xxxx.fileprovider" android:grantUriPermissions="true" android:exported="false" > <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> 这个标签放到AndroidManifest的<application>标签里面
Android_Java_IOS 2017-03-02
  • 打赏
  • 举报
回复
解决了吗楼主哥哥!
gys19921114 2016-12-16
  • 打赏
  • 举报
回复
我就是看着网上教程来的,始终提示我路径有问题,FileProvider不会用,求链接
怀君 2016-12-16
  • 打赏
  • 举报
回复
网上有教程,大兄弟。

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧