这只是权宜之计,送给需要的小伙伴,因为google在4.2上把一部分接口给去掉了,所以可能在4.2的设备上上传可能会有问题,三星的一部分手机把H5直接上传屏蔽了,所以在三星的一部分设备可能也会有问题。要想适配所有设备,最好是H5调原生的JS上传后,原生的JS在回调H5的方法,告诉H5完成上传,并把链接给进去。
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* 待文件上传H5
*
*
*/
@SuppressLint("SetJavaScriptEnabled")
public class WebViewFileContentFragment extends Fragment {
@ViewInject(id = )
private WebView wv;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = (.fragment_darenapply_content,
container, false);
// 初始化注解
(this, view);
init();
return view;
}
private void init() {
String uri = getActivity().getIntent().getStringExtra("uri");
initWebView(wv);
(uri);
}
private final static Object methodInvoke(Object obj, String method,
Class<?>[] parameterTypes, Object[] args) {
try {
Method m = ().getMethod(method,
new Class[] { });
(obj, args);
} catch (Exception e) {
();
}
return null;
}
@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" })
private void initWebView(WebView webView) {
WebSettings settings = ();
(true);
(true);
(true);
(WebSettings.LOAD_NO_CACHE);
(true);
(true);
(true);
// (true);
methodInvoke(settings, "setPluginsEnabled",
new Class[] { }, new Object[] { true });
// ();
methodInvoke(settings, "setPluginState",
new Class[] { },
new Object[] { });
// (true);
methodInvoke(settings, "setPluginsEnabled",
new Class[] { }, new Object[] { true });
// (true);
methodInvoke(settings, "setAllowUniversalAccessFromFileURLs",
new Class[] { }, new Object[] { true });
// (true);
methodInvoke(settings, "setAllowFileAccessFromFileURLs",
new Class[] { }, new Object[] { true });
(View.SCROLLBARS_INSIDE_OVERLAY);
();
();
(true);
().setDefaultTextEncodingName("utf-8");
(new WebViewClient());
(new MyWebChromeClient());
// (downloadListener);
}
@SuppressWarnings("static-access")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Uri result = null;
if (resultCode == getActivity().RESULT_CANCELED) {
} else {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage) {
return;
}
result = ();
// ------------开始取得文件真实路径
String img_path = (getActivity(),
result);
File file = new File(img_path);
if ((img_path))// 中文处理
{
Bitmap bitmap;
String status = ();
if ((Environment.MEDIA_MOUNTED)) {
try {
bitmap = (
getActivity().getContentResolver(), result);
String tempFile = (
getActivity(), ()
+ ".jpg", bitmap);
file = new File(tempFile);
} catch (Exception e) {
();
("转换异常");
}
} else {
(getActivity(),
"没有sd卡就不允许选择路径有中文的图片");
}
}
result = (file);
}
}
// ------------结束取得文件真实路径
(result);
mUploadMessage = null;
}
class MyWebChromeClient extends WebChromeClient {
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
// 如果朋友们要限制上传到服务器的图片类型时可以直接写如:"image/jpeg 、 image/png等的类型"
(
.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(pickIntent, FILECHOOSER_RESULTCODE);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
(
.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(pickIntent, FILECHOOSER_RESULTCODE);
}
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
// 如果朋友们要限制上传到服务器的图片类型时可以直接写如:"image/jpeg 、 image/png等的类型"
(
.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(pickIntent, FILECHOOSER_RESULTCODE);
}
}
public void onResume() {
();
(()); // 统计页面
}
public void onPause() {
();
(());
}
}