android调用js上传文件异常问题

lufengdie 发布于 2015/06/04 17:21
阅读 1K+
收藏 0


需求: 在android客户端内用webview加载服务url, 类似一个壳,实现上传文件功能。

web代码片段

<div id="courseWare_dialog" class="hide">
    <form role="form" class="form-horizontal " style="background:none;">
        <div class="form-group">
            <label class="col-sm-3 control-label no-padding-right" >上传文件:</label>
            <div class="col-sm-8">
                <input type="file" id="resfile" name="resfile" accept="<%=aceptType%>"/>
            </div>
        </div>
        
        <div class="clearfix no-margin">
            <div class="col-md-9">
                <a class="mybtn" onclick="uploadRes();">
                    提交
                </a>
                &nbsp; &nbsp; &nbsp;
                <a class="mybtn gely" onclick="$('#courseWare_dialog').dialog('close');">
                    关闭
                </a>
            </div>
        </div>
    </form>
</div>


<input type="file" id="resfile" name="resfile" accept="<%=aceptType%>"/>
点击 input触发文件选择器。



android代码片段:

webView.setWebChromeClient(new WebChromeClient() {
			
            // For Android < 3.0  
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {  
                openFileChooser(uploadMsg, "");  
            }  
			
            // For Android 3.0+  
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {    
            	Log.e("openFileChooser", "openFileChooser==========");
            	if (mUploadMessage != null) return;  
            	mUploadMessage = uploadMsg;     
            	startActivityForResult(createDefaultOpenableIntent(), FILECHOOSER_RESULTCODE);
            }  
            
	         // For Android  > 4.1.1  
			public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
				Log.e("openFileChooser", "openFileChooser===");
	            openFileChooser(uploadMsg, acceptType);
			}
			
		});


startActivityForResult(createDefaultOpenableIntent(), FILECHOOSER_RESULTCODE);

显示文件选择器


将需要上传的文件交给 ValueCallback回调:

@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Uri result = null;
		if(requestCode == FILECHOOSER_RESULTCODE && resultCode == Activity.RESULT_OK) {  
            if (null == mUploadMessage) return;  
                     String resultPath = cameraFilePath;
                     if (data == null) {
	                     File uploadFile = new File(cameraFilePath);
	                     if (uploadFile.exists()) {
	                         result = Uri.fromFile(uploadFile);
	                         // Broadcast to the media scanner that we have a new photo
	                         // so it will be added into the gallery for the user.
                         	 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
	                     }
                      }else{
                     	  resultPath = getPath(getApplicationContext(), data.getData());
                      }
                     result = Uri.fromFile(new File(resultPath));
                     mUploadMessage.onReceiveValue(result);  
                     mUploadMessage = null;
           }
	}




正常拍照或者从文档里选择文件上传都没有问题,

但是,如果点击选择器以外的区域,选择器会消失(类似alertDialog.setCanceledOnTouchOutside(true);),再次点击web上的选择按钮就无法显示选择器。


各位来往的大小神,这是什么原因呢?  站求!!!




加载中
0
lufengdie
lufengdie


问题在于:点击选择器outside时表示取消选择,onActivityResult中resultCode应该是Activity.RESULT_CANCELED,此时应该给ValueCallback传null。


因此在onActivityResult中resultCode==Activity.RESULT_CANCELED时,调用下面代码即可

mUploadMessage.onReceiveValue(result);  
mUploadMessage = null;


即:

@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Uri result = null;
		if(requestCode == FILECHOOSER_RESULTCODE && resultCode == Activity.RESULT_OK) {  
            if (null == mUploadMessage) return;  
                     String resultPath = cameraFilePath;
                     if (data == null) {
	                     File uploadFile = new File(cameraFilePath);
	                     if (uploadFile.exists()) {
	                         result = Uri.fromFile(uploadFile);
	                         // Broadcast to the media scanner that we have a new photo
	                         // so it will be added into the gallery for the user.
                         	 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
	                     }
                      }else{
                     	  resultPath = getPath(getApplicationContext(), data.getData());
                      }
                     result = Uri.fromFile(new File(resultPath));
                     mUploadMessage.onReceiveValue(result);  
                     mUploadMessage = null;
           }
	}






返回顶部
顶部