之前在eclipse上打包的项目在5.0的手机上正常,这俩天项目转到android studio 上,5.0之上的系统文件上传反而无反应,之后根据网络资料整理解决,代码如下
// 5.0 +文件上传
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
if (mUploadMessages != null) {
//mUploadMessages.onReceiveValue(null);
//这行代码会导致文件只能上传一次,需注意
mUploadMessages = null;
}
mUploadMessages = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(mMainActivity.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
// Error occurred while creating the File
Log.e("WebViewSetting", "Unable to create Image File", ex);
}// Continue only if the File was successfully created
if (photoFile != null) {
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent[] intentArray;
if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else {
intentArray = new Intent[0];
}Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
mMainActivity.startActivityForResult(chooserIntent, mMainActivity.INPUT_FILE_REQUEST_CODE);
return true;
}
然后是返回接收
/**
* 返回文件选择
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == FILECHOOSER_RESULTCODE) {
mUploadMessage = chromeClient.getmUploadMessage();
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}elseif(requestCode == INPUT_FILE_REQUEST_CODE){
mUploadMessages = chromeClient.getmUploadMessage2();
String mCameraPhotoPath = chromeClient.getCameraPhotoPath();
if (null == mUploadMessages) return;
// 5.0的回调
Uri[] results = null;
// Check that the response is a good one
if (resultCode == Activity.RESULT_OK) {
if (intent == null) {
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
} else {
String dataString = intent.getDataString();
ClipData clipData = https://www.it610.com/article/intent.getClipData();
if(clipData != null){
results = new Uri[clipData.getItemCount()];
for (int i = 0;
i < clipData.getItemCount();
i++) {
ClipData.Item item = clipData.getItemAt(i);
results[i] = item.getUri();
}
}
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
mUploadMessages.onReceiveValue(results);
mUploadMessages = null;
} else {
super.onActivityResult(requestCode, resultCode, intent);
return;
}
}
其他例如4.1+的系统版本(包含4.4),部分机型也是无法上传文件,尚未解决,解决后更新,如有了解的朋友望不吝赐教。
【android studio 打包webview 5.0文件上传问题】