Android網頁WebView圖片文件上傳的問題

在安卓下,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);
把這句刪掉就行;










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章