webView 混淆上传无响应问题

来源:互联网 发布:java 遗传算法排课 编辑:程序博客网 时间:2024/06/05 17:10
涉及到不同版本的API的兼容性问题,openFileChooser需要实现以下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.  // 添加事件  
  2.     mWebView.setWebChromeClient(new WebChromeClient() {  
  3.   
  4.         @SuppressWarnings("unused")  
  5.         public void openFileChooser(ValueCallback<Uri> uploadFile) {  
  6.             uploadFile(uploadFile);  
  7.         }  
  8.   
  9.         @SuppressWarnings("unused")  
  10.         public void openFileChooser(ValueCallback<Uri> uploadFile,  
  11.                 String acceptType) {  
  12.             uploadFile(uploadFile);  
  13.         }  
  14.   
  15.         @SuppressWarnings("unused")  
  16.         public void openFileChooser(ValueCallback<Uri> uploadFile,  
  17.                 String acceptType, String capture) {  
  18.             uploadFile(uploadFile);  
  19.         }  
  20.   
  21.     });  
  22.   
  23.      // 上传文件  
  24.     private ValueCallback<Uri> mUploadMessage;  
  25.     private void uploadFile(ValueCallback<Uri> uploadFile) {  
  26.     mUploadMessage = uploadFile;  
  27.     Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
  28.     i.addCategory(Intent.CATEGORY_OPENABLE);  
  29.     i.setType("image/*");  
  30.     startActivityForResult(Intent.createChooser(i, "File Chooser"),  
  31.             FILE_CHOOSER_CODE);  
  32. }  
  33.   
  34.       // 结果回传  
  35.       @Override  
  36. protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  37.     super.onActivityResult(requestCode, resultCode, data);  
  38.     if (requestCode == FILE_CHOOSER_CODE) {  
  39.         if (mUploadMessage == null)  
  40.             return;  
  41.         if (data != null && resultCode == RESULT_OK) {  
  42.             Uri result = data.getData();  
  43.             if (result != null) {  
  44.                 mUploadMessage.onReceiveValue(result);  
  45.                 mUploadMessage = null;  
  46.             }  
  47.         }  
  48.     }  


以上是文件上传的过程,重写不同版本的openFileChooser实现上传,然后在onActivityResult中
onReceiveValue将结果回馈给 Js端。以下是混淆相关:

    1)为了避免Js调用在混淆后失效,需要添加以下混淆(将Js调用类保持不混淆)
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -keepclassmembers class com.umai.taok.manager.JSBridge$AndroidAPI {  
  2.            public *;  
  3.       }  
  4.     -keepclassmembers class com.umai.taok.manager.ClientAPI {  
  5.           public *;  
  6.      }  
  7.      -keep class com.umai.taok.manager.JSBridge$AndroidAPI {  
  8.           public *;  
  9.      }  
  10.      -keep class com.umai.taok.manager.ClientAPI {  
  11.          public *;  
  12.       }  
  13.   
  14.     -keepattributes *Annotation*    
  15.     -keepattributes *JavascriptInterface*   

   2)为了使WebView上传文件能顺利执行,需要添加以下混淆(保持openFileChooser方法不混淆)
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. -keepclassmembers class * extends android.webkit.WebChromeClient {  
  2.             public void openFileChooser(...);  
  3.       }  

关于文件上传只执行第一次解决办法

    经实验得知,关于openFileChooser上传文件只执行第一次问题,引起的原因是在文件第二次选择返回结果后,或者在选择文件取消操作之后,不能有效的向Js传递结果。所以对应的解决办法就是:
1)在传递对象为空时,将null的结果传递到Js端
       
mUploadMessage.onReceiveValue(null);
      mUploadMessage = null;
2)在onActivityResult中判断取消动作,并将null结果传递到Js端 
       
if (resultCode == 0) {
   // 取消
   mUploadMessage.onReceiveValue(null);
   mUploadMessage = null;
       }

0 0
原创粉丝点击