杭州Android培训
达内杭州Android培训中心

13732203138

热门课程

H5界面调用原生

  • 时间:2018-06-22 14:46
  • 发布:杭州android中心
  • 来源:疑难解答

下面,对这个步骤进行一下详细的说明:

步骤:

1,在manifest中添加权限:

<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.hardware.camera"/>

2,对webview进行一些基本权限设置

webSettings.setJavaScriptEnabled(true); webSettings.setDefaultTextEncodingName("UTF-8"); webSettings.setAllowContentAccess(true); // 是否可访问Content Provider的资源,默认值 true webSettings.setAllowFileAccess(true); // 是否可访问本地文件,默认值 true

3,初始化WebChromeClient

1>实现WebView的方法

//扩展浏览器上传文件 //3.0++版本 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { openFileChooserImpl(uploadMsg); } //3.0--版本 public void openFileChooser(ValueCallback<Uri> uploadMsg) { openFileChooserImpl(uploadMsg); } public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { openFileChooserImpl(uploadMsg); } //5.0++版本 @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { onenFileChooseImpleForAndroid(filePathCallback); return true; }

2>openFileChooserImpl方法

private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent j = new Intent(Intent.ACTION_GET_CONTENT); Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); }

3> onenFileChooseImpleForAndroid(filePathCallback);

private void onenFileChooseImpleForAndroid(ValueCallback<Uri[]> filePathCallback) { mUploadMessageForAndroid5 = filePathCallback; final List<Intent> cameraIntents = new ArrayList<Intent>(); File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/" + SystemClock.currentThreadTimeMillis() + ".jpg"); imageUri = Uri.fromFile(fileUri); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { imageUri = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName() + ".fileprovider", fileUri);//通过FileProvider创建一个content类型的Uri } Intent intentCamera = new Intent(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intentCamera.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件 } intentCamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE); //将拍照结果保存至photo_file的Uri中,不保留在相册中 intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); cameraIntents.add(intentCamera); Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); contentSelectionIntent.setType("*/*"); Intent i = new Intent(Intent.ACTION_CHOOSER); i.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); i.putExtra(Intent.EXTRA_TITLE, "File Chooser"); Intent chooserIntent = Intent.createChooser(i, "Image Chooser"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{})); getActivity().startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5); }

4>onActivityResult进行回调

public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5) { if (null == mUploadMessageForAndroid5) return; Uri result = (intent == null || resultCode != RESULT_OK) ? null : intent.getData(); if (result != null) { mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result}); } else { Uri[] results = new Uri[]{imageUri}; mUploadMessageForAndroid5.onReceiveValue(results); } mUploadMessageForAndroid5 = null; } }

上一篇:选择android开发培训班的原因
下一篇:安卓手机Root教程
选择城市和中心
贵州省

广西省

海南省