开源TakePhoto调用相机出现UndeclaredThrow

2018-07-09  本文已影响280人  叨码

首先简单介绍下TakePhoto,用过的童鞋肯定很熟悉了,是一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库。

java.lang.reflect.UndeclaredThrowableException
        at $Proxy9.onPickFromCapture(Unknown Source)
        at com.sd.ele.activity.AddHazardActivity.takePicture(AddHazardActivity.java:341)
        at com.sd.ele.activity.AddHazardActivity.access$1200(AddHazardActivity.java:80)
        at com.sd.ele.activity.AddHazardActivity$9.onItemClick(AddHazardActivity.java:317)
        at com.sd.ele.widget.SelectDialog.onItemClick(SelectDialog.java:181)
        at android.widget.AdapterView.performItemClick(AdapterView.java:310)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1274)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3297)
        at android.widget.AbsListView$5.run(AbsListView.java:4229)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:159)
        at android.app.ActivityThread.main(ActivityThread.java:6364)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1096)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:883)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at com.sd.ele.photo.permission.TakePhotoInvocationHandler.invoke(TakePhotoInvocationHandler.java:41)
        at java.lang.reflect.Proxy.invoke(Proxy.java:813)
        at $Proxy9.onPickFromCapture(Unknown Source) 
        at com.sd.ele.activity.AddHazardActivity.takePicture(AddHazardActivity.java:341) 
        at com.sd.ele.activity.AddHazardActivity.access$1200(AddHazardActivity.java:80) 
        at com.sd.ele.activity.AddHazardActivity$9.onItemClick(AddHazardActivity.java:317) 
        at com.sd.ele.widget.SelectDialog.onItemClick(SelectDialog.java:181) 
        at android.widget.AdapterView.performItemClick(AdapterView.java:310) 
        at android.widget.AbsListView.performItemClick(AbsListView.java:1274) 
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3297) 
        at android.widget.AbsListView$5.run(AbsListView.java:4229) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:159) 
        at android.app.ActivityThread.main(ActivityThread.java:6364) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1096) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:883) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.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)
        at com.sd.ele.photo.utils.TUriParse.getUriForFile(TUriParse.java:88)
        at com.sd.ele.photo.utils.TUriParse.convertFileUriToFileProviderUri(TUriParse.java:42)
        at com.sd.ele.photo.app.TakePhotoImpl.onPickFromCapture(TakePhotoImpl.java:365)
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.sd.ele.photo.permission.TakePhotoInvocationHandler.invoke(TakePhotoInvocationHandler.java:41) 
        at java.lang.reflect.Proxy.invoke(Proxy.java:813) 
        at $Proxy9.onPickFromCapture(Unknown Source) 
        at com.sd.ele.activity.AddHazardActivity.takePicture(AddHazardActivity.java:341) 
        at com.sd.ele.activity.AddHazardActivity.access$1200(AddHazardActivity.java:80) 
        at com.sd.ele.activity.AddHazardActivity$9.onItemClick(AddHazardActivity.java:317) 
        at com.sd.ele.widget.SelectDialog.onItemClick(SelectDialog.java:181) 
        at android.widget.AdapterView.performItemClick(AdapterView.java:310) 
        at android.widget.AbsListView.performItemClick(AbsListView.java:1274) 
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3297) 
        at android.widget.AbsListView$5.run(AbsListView.java:4229) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:159) 
        at android.app.ActivityThread.main(ActivityThread.java:6364) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1096) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:883) 

程序原本在魅族手机上开发,一切正常,但是尝试运行在华为和锤子手机时,就出现了如上错误,首先根据日志定位到错误代码在TUriParse.java:

/**
     * 创建一个用于拍照图片输出路径的Uri (FileProvider)
     * @param context
     * @return
     */
public static Uri getUriForFile(Context context, File file) {
        return FileProvider.getUriForFile(context,TConstant.getFileProviderName(context), file);
   }

进入TConstant.getFileProviderName(context)

  public final static String getFileProviderName(Context context) {
        return context.getPackageName() + ".fileprovider";
    }

另外查了下github上issues,里面的确有人碰到了这类问题--(https://github.com/crazycodeboy/TakePhoto/issues/360)
有网友解答了一句话:[其实是7.0配置FileProvider跟TakePhoto的resources冲突导致的,添加即可]
是不是很懵逼,添加什么?怎么添加?却没有说。
另外一个issues也是关于此问题的:(https://github.com/crazycodeboy/TakePhoto/issues/390)
也是一句话:[我的解决办法是把自己的provider标签的authorities属性值改成${applicationId}.fileprovider即可]
对这类回答,可能大多数人都和一样,是又爱又恨啊。好在给了我们一个思路,问题应该出在Android7.0对文件的访问,框架使用StrictMode Api禁止我们的应用对外部(跨越应用分享)公开file://,在访问文件时需要增加FileProvider授权,这里不再赘述这个问题。所以就需要我们添加必要的配置才行,解决办法就是:
AndroidManifest.xml文件里还需要加入这段:

<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>

另外格外注意的一点就是android:authorities="${applicationId}.fileprovider"中的fileprovider必须和

public final static String getFileProviderName(Context context) {
        return context.getPackageName() + ".fileprovider";
    }

中的fileprovider保持一致。
再有就是需要添加一个配置xml文件file_path.xml对应你配置里的android:resource="@xml/file_paths"即可:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
        <!--name 是起的别名(随便起)   path是要共享的文件夹 -->
        <external-path
            name="files_root"
            path="Android/data/com.ele.lookover/" />
        <external-path
          <!--这里就不能随便起,path是你的照片存储根路径external-path
,不然,拍照后的回调会有问题,报找不到图片路径地址 -->
            name="/storage/emulated/0/" 
            path="." />
    </paths>
</resources>

这样,再次运行,问题解决。

参考文章:https://www.cnblogs.com/cuma/p/8108400.html

上一篇下一篇

猜你喜欢

热点阅读