leoren
leoren
  • 发布:2017-07-24 11:21
  • 更新:2017-09-18 18:33
  • 阅读:3774

android7.0 本地打包后,相机调起失败 exposed beyond app through ClipData.Item.getUri()

分类:HTML5+

"file:///storage/emulated/0/Android/data/com.HBuilder.integrate/apps/Lockinlife/doc/gallery/1500865738774.jpg exposed beyond app through ClipData.Item.getUri()

function getImage() {  
            var thisTime = new Date();  
            thisTime = thisTime.getTime();  
            var imgName = ''+ thisTime + '.jpg';  
            console.log(imgName);  
            var c = plus.camera.getCamera();  
            c.captureImage(function(e) {  
                plus.io.resolveLocalFileSystemURL(e, function(entry) {  
                    var s = entry.toLocalURL();  
                    plus.storage.setItem("img_geader_src",s);  
                    plus.zip.compressImage({  
                            src:s,  
                            dst:s,  
                            overwrite: true,  
                            format:"jpg",  
                            width: "30%",  
                            quality:15,  
                        },  
                        function(success) {  
                            GetBase64Code2(s);  
                            console.log(s);  
                            document.getElementById("l-e-header").src = s;  
                            /*setTimeout(function() {  
                                console.log(pic);  
                            },2000)*/  

//                          alert("Compress success cut!");  
                            plus.nativeUI.toast("车位照片选取成功!");  
                        },function(error) {  
//                          alert("Compress error cut!");  
                            plus.nativeUI.toast("车位照片选取失败!");  
                    });  
                    document.getElementById("l-e-header").src = s;  
                }, function(e) {  
                    console.log("读取拍照文件错误:" + e.message);  
                    plus.nativeUI.toast("读取拍照文件错误:" + e.message);  
                });  
            }, function(s) {  
                console.log("error" + s);  
                console.log(s);  
                plus.nativeUI.toast("error" + s);  
            }, {  
                filename: "_doc/"  
            })  
        }
<uses-permission android:name="android.permission.CAMERA"/>
2017-07-24 11:21 负责人:无 分享
已邀请:
Trueno

Trueno

楼主解决了吗?

  • leoren (作者)

    没有啊,你也有这个问题吗?

    2017-07-25 13:27

  • Trueno

    回复 leoren:我解决了,暂时只能把targetSdkVersion改成23,7.0api改了

    2017-07-25 18:54

坛子

坛子

两个解决方案:

  1. 重写 CameraFeatureImpl.java,具体参考 http://blog.csdn.net/huangxiaoguo1/article/details/52830015,但是这个必须去修改jar包,比较复杂。

  2. 第二个方案是不安全,但是速度较快,就是重写 io.dcloud.application.Application比如重新写个 io.dcloud.application.NewApplication,然后在onCreate方法中,加上如下代码:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();

然后更新 AndroidManifest.xml
<application
android:name="io.dcloud.application.NewApplication"

1602827193@qq.com

1602827193@qq.com

同问,我也遇到这个问题,楼主解决了吗?

  • Trueno

    上个回复有两条评论,我已经解决了

    2017-08-16 11:53

xj01132@126.com

xj01132@126.com

同问,楼主有没有解决呢?

  • leoren (作者)

    你试试二楼的回复:“targetSdkVersion改成23”

    2017-08-21 16:38

  • xj01132@126.com

    试了,不行。

    2017-09-03 10:52

xj01132@126.com

xj01132@126.com

官方都没人理呢?

745760410@qq.com

745760410@qq.com - it男

这个问题,我也问了,不过貌似都没有人解决,最后没办法,自己搞定了!说下我的思路,顺便直接上代码,有兴趣想继续深究的或者想互相学习的,可以加入这个QQ群 : 488505459

1.既然是本地打包,调不到,那为啥不换个思路,查一下android 7.0 调用摄像头的问题呢?这个网上有说明的,都大同小异,我就不说明了!

  1. 你可以去看下,官方提供的 调用摄像头的代码 camera.jar(.class 文件怎么查看,工具建议使用 jd-gui.exe),你会发现(大概在83行位置) Intent localIntent = new Intent("android.media.action.IMAGE_CAPTURE"); Uri localUri = Uri.fromFile((File)localObject); localIntent.putExtra("output", localUri); 这个是统一的,并没有区分 android版本号,所以问题就在这啦。
  2. 也许你会想办法,像在这里加入几行代码,区分版本号,但是就我实验的结果,貌似官方提供的camera.jar 是加过密的,而且反编译出来的东西有很多错误!所以,你既然已经本地打包了,说明你肯定是
    要自己开发第三方插件,都自己开发第三方插件了,为啥不换个思路,掉摄像头的代码,自己来写呢?顺便提一下,关于如何开发第三方插件,如何5跳原生,原生跳h5 ,我写了一篇博客,可能不是太详细,但大家可以看下 http://blog.csdn.net/u010978381 。
    4.总共涉及到两个类,一个接口类,这个开发第三方插件必须的,涉及到的代码:
    //调用摄像头
    public void PluginGetCameraFunction(final IWebview pWebview, JSONArray array) {
    final String CallBackID = array.optString(0);
    final IApp localIApp = pWebview.obtainFrameView().obtainApp();
    final JSONArray newArray = new JSONArray();
    final String str1=array.optString(1); //文件名称

    PermissionUtil.usePermission(localIApp.getActivity(), false, "CAMERA", new PermissionUtil.StreamPermissionRequest(localIApp)  
    {  
        public void onGranted(String paramAnonymousString) {  
            try {  
                //Object localObject = new File(pWebview.getContext().getExternalCacheDir(), "_doc/output_image.jpg");  
                final Object localObject = new File(pWebview.getContext().getExternalCacheDir(), str1);  
    
                final  File localFile = ((File)localObject).getParentFile();  
                if (!localFile.exists()) {  
                    localFile.mkdirs();  
                }  
                localIApp.registerSysEventListener(new ISysEventListener()  
                   {  
                       public boolean onExecute(ISysEventListener.SysEventType paramAnonymous2SysEventType, Object paramAnonymous2Object) {  
                           Object[] arrayOfObject = (Object[])paramAnonymous2Object;  
                           int i = ((Integer)arrayOfObject[0]).intValue();  
                           int j = ((Integer)arrayOfObject[1]).intValue();  
                           if ((paramAnonymous2SysEventType == ISysEventListener.SysEventType.onActivityResult) &&  
                                   (i == a.a))  
                           {  
                               if (j == -1) {  
                                    String str=((File)localObject).getAbsolutePath();  
                                    newArray.put(str);  
                                   JSUtil.execCallback(pWebview, CallBackID, newArray, JSUtil.OK, false);  
                               } else {  
                                   String str = DOMException.toJSON(11, "resultCode is wrong");  
                                   newArray.put(str);  
                                   JSUtil.execCallback(pWebview, CallBackID, newArray, JSUtil.ERROR, false);  
                               }  
                               localIApp.unregisterSysEventListener(this, paramAnonymous2SysEventType);  
                           }  
                           return false;  
                       }  
                   }, ISysEventListener.SysEventType.onActivityResult);  
    
                int currentapiVersion = android.os.Build.VERSION.SDK_INT;  
                Intent localIntent = new Intent("android.media.action.IMAGE_CAPTURE");  
    
                if (currentapiVersion<24){  
                    Uri localUri = Uri.fromFile((File)localObject);  
                    localIntent.putExtra("output", localUri);  
                    pWebview.getActivity().startActivityForResult(localIntent, a.a);  
                }else {  
                    ContentValues contentValues = new ContentValues(1);  
                    contentValues.put(MediaStore.Images.Media.DATA, ((File)localObject).getAbsolutePath());  
                    Uri uri = pWebview.getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);  
                    localIntent.putExtra("output", uri);  
                    pWebview.getActivity().startActivityForResult(localIntent, a.a);  
                }  
            } catch (Exception localException) {  
    
            }  
        }  
        public void onDenied(String paramAnonymousString)  
        {  
            String str = DOMException.toJSON(11, "No Permission");  
            newArray.put(str);  
            JSUtil.execCallback(pWebview, CallBackID, newArray, JSUtil.OK, false);  
        }  
    });  

    }
    第二个类 a.java
    /**

    • Created by jwj on 2017/9/13.
      */
      import android.hardware.Camera;
      import io.dcloud.common.adapter.util.DeviceInfo;
      import java.util.List;

class a
{
protected static int a = 1;
protected static int b = 2;

List<Camera.Size> c = null;  
List<Integer> d = null;  
List<Camera.Size> e = null;  

a(int paramInt)  
{  
    try  
    {  
        Camera localCamera = null;  
        if ((paramInt == 2) && (DeviceInfo.sDeviceSdkVer >= 9)) {  
            for (int i = 0; i < Camera.getNumberOfCameras(); i++) {  
                Camera.CameraInfo localCameraInfo = new Camera.CameraInfo();  
                Camera.getCameraInfo(i, localCameraInfo);  
                if (localCameraInfo.facing == 1) {  
                    localCamera = Camera.open(i);  
                    break;  
                }  
            }  
        }  
        if (localCamera == null) {  
            localCamera = Camera.open();  
        }  
        if (DeviceInfo.sDeviceSdkVer >= 11) {  
            this.c = localCamera.getParameters().getSupportedVideoSizes();  
        }  
        this.e = localCamera.getParameters().getSupportedPictureSizes();  
        if (DeviceInfo.sDeviceSdkVer >= 8) {  
            this.d = localCamera.getParameters().getSupportedPictureFormats();  
        }  
        localCamera.release();  
    } catch (Exception localException) {  
        localException.printStackTrace();  
    }  
}  

protected String a()  
{  
    String str = "(function(){return{supportedImageResolutions : %s,supportedVideoResolutions : %s,supportedImageFormats : %s,supportedVideoFormats : %s};})();";  

    String[] arrayOfString = d();  
    str = String.format(str, new Object[] { c(), b(), arrayOfString[0], arrayOfString[1] });  

    return str;  
}  

private String b()  
{  
    String str = "[]";  
    if ((this.c != null) && (DeviceInfo.sDeviceSdkVer >= 11)) {  
        List localList = this.c;  
        str = b(localList);  
    }  
    return str;  
}  

private String c()  
{  
    String str = "[]";  
    if (this.e != null) {  
        List localList = this.e;  
        str = b(localList);  
    }  
    return str;  
}  

private String[] d()  
{  
    String[] arrayOfString = null;  
    if (this.d != null) {  
        List localList = this.d;  
        arrayOfString = a(localList);  
    }  
    if (arrayOfString == null) {  
        arrayOfString = new String[2];  
        arrayOfString[0] = "['jpg']";  
        arrayOfString[1] = "['mp4']";  
    }  
    return arrayOfString;  
}  

private String[] a(List<Integer> paramList)  
{  
    String[] arrayOfString = new String[2];  
    arrayOfString[0] = "['jpg']";  
    arrayOfString[1] = "['mp4']";  

    return arrayOfString;  
}  

private String b(List<Camera.Size> paramList)  
{  
    String str = "[]";  
    int i = paramList.size();  
    if ((paramList != null) && (i > 1)) {  
        StringBuffer localStringBuffer = new StringBuffer();  
        localStringBuffer.append("[");  
        for (int j = 0; j < i; j++) {  
            localStringBuffer.append("'" + ((Camera.Size)paramList.get(j)).width + "*" + ((Camera.Size)paramList.get(j)).height + "'");  
            if (j != i - 1) {  
                localStringBuffer.append(",");  
            }  
        }  
        localStringBuffer.append("]");  
        str = localStringBuffer.toString();  
    }  
    return str;  
}  

}
前台接受的代码,我们仍然是在 function getImage(obj) {}方法里来完成,不过此时的代码是这样
plus.plugintest.PluginGetCameraFunction("_doc/"+obj+".jpg", function(e) {
var s = e[0] + "?version=" + new Date().getTime();
document.getElementById(obj).src = s;

                },function(s){  
                    plus.ui.toast("error:" + s.message);  
                });  

"_doc/"+obj+".jpg" 这个表示图片名称,自己传递到后台的! 因为是在开发项目,所以写的很匆忙,有些代码可能冗余,大家见谅一下呀,最后说一句,欢迎加入QQ群,一起学习呀!

745760410@qq.com

745760410@qq.com - it男

传上去之后,格式太乱了,所以直接发个包,希望对大家有帮助!

移动达人

移动达人

不错。

要回复问题请先登录注册