在安卓下,webview上传图片点击是没用的,需要自己写一下.
网上关于这个的很多,基本都是抄来抄去,没什么用的.
这个日期比较新,而且能用
http://blog.csdn.net/djcken/article/details/46379929#comments
就是自定义实现 WebChromeClient
然后重写 openFileChooser 方法,获取 ValueCallback<Uri> valueCallback
当然,要注意不同版本的区别.,但5.0+的项目,就不能用了.回复里有答案.
就是重写 onShowFileChooser 但这次是 ValueCallback<Uri[]> valueCallback
虽然麻烦点,但能实现
使用腾讯X5内核http://x5.tencent.com/ 不用去考虑那些版本兼容,只要重写openFileChooser 获取 ValueCallback<Uri> valueCallback
在这些方法 可以直接跳转相册或相机,也可以弹出对话框来选择;
只需在 onActivityResult 方法获取 android.net.Uri 对象就行
跳转相册:
Intent intent = Intent(Intent., ); intent.setDataAndType(MediaStore.Images.Media., ); startActivityForResult(15);
相册结果: Uri uri = data.getData();
跳转相机 :
private Uri photoUri;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); ContentValues values = new ContentValues(); photoUri = context.getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(16); 相机结果: 直接 photoUri 就行 把图片给网页,用刚才获取的valueCallback valueCallback.onReceiveValue(uri); 如果用户取消就 valueCallback.onReceiveValue(null); 然后每次都要 uriValueCallback = null; 这些和网上的基本一样,但今天被一个问题坑了很久... 错误日志是这样的 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=18, result=0, data=null} to activity {com.tugouzhong.micromall/com.tugouzhong.user.WebActivity}: java.lang.IllegalStateException: showFileChooser result was already called at android.app.ActivityThread.deliverResults(ActivityThread.java:3609) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3652) at android.app.ActivityThread.access$1300(ActivityThread.java:159) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5313) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1116) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:809) Caused by: java.lang.IllegalStateException: showFileChooser result was already called at com.tencent.smtt.webkit.x$14.a(Unknown Source) at com.tencent.smtt.webkit.x$14.onReceiveValue(Unknown Source) at com.tencent.smtt.sdk.o.a(Unknown Source) at com.tencent.smtt.sdk.o.onReceiveValue(Unknown Source) at com.test.user.WebActivity.onActivityResult(WebActivity.java:214) at android.app.Activity.dispatchActivityResult(Activity.java:6240) at android.app.ActivityThread.deliverResults(ActivityThread.java:3605) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3652) at android.app.ActivityThread.access$1300(ActivityThread.java:159) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5313) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1116) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:809) 这个是用的x5内核的日志,用系统的也是java.lang.IllegalStateException这个问题. 网上找竟然没发现同样问题的人.... 自己看了下,原来是在openFileChooser方法里调用了 super.openFileChooser(valueCallback, acceptType, captureType); 把这句删掉就行;
本文出自 “苍蝇学android” 博客,请务必保留此出处http://qq445493481.blog.51cto.com/9545543/1856943
原文地址:http://qq445493481.blog.51cto.com/9545543/1856943