欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android 开发 web页面调取相册相机 上传图片

程序员文章站 2022-03-27 21:40:15
android 开发 web页面调取相册相机 上传图片 private Context context = this; private ValueCallback mUpl...

android 开发 web页面调取相册相机 上传图片

private Context context = this;
private ValueCallback mUploadMessage;// 表单的数据信息
private ValueCallback mUploadCallbackAboveL;
private Uri imageUri;
private WebView activity_webview;
private final static int FILECHOOSER_RESULTCODE = 1;// 表单的结果回调

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initDate(); initInfo(); } private void initDate() { activity_webview = (WebView) findViewById(R.id.activity_webview); loadUrl("https://192.168.1.67/xinfupin/ab_web/web_renzheng/rz_user/" + FuPinAppliction.UserPF.readUserId() + "/" + FuPinAppliction.UserPF.readSession_Key()); activity_webview.setWebChromeClient(new WebChromeClient() { @Override public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) { mUploadCallbackAboveL = filePathCallback; take(); return true; } public void openFileChooser(ValueCallback uploadMsg) { mUploadMessage = uploadMsg; take(); } public void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; take(); } public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; take(); } }); activity_webview.getSettings().setSaveFormData(false); activity_webview.clearFormData(); }

private void take() { File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp"); // Create the storage directory if it does not exist  if (!imageStorageDir.exists()) { imageStorageDir.mkdirs(); } File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); imageUri = Uri.fromFile(file); final List cameraIntents = new ArrayList(); final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); final PackageManager packageManager = getPackageManager(); final List listCam = packageManager.queryIntentActivities(captureIntent, 0); for (ResolveInfo res : listCam) { final String packageName = res.activityInfo.packageName; final Intent i = new Intent(captureIntent); i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); i.setPackage(packageName); i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); cameraIntents.add(i); } Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); Intent chooserIntent = Intent.createChooser(i, "Image Chooser"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{})); this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage && null == mUploadCallbackAboveL) return; Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); if (mUploadCallbackAboveL != null) { onActivityResultAboveL(requestCode, resultCode, data); } else if (mUploadMessage != null) { Log.e("result", result + ""); if (result == null) { mUploadMessage.onReceiveValue(imageUri); mUploadMessage = null; } else { mUploadMessage.onReceiveValue(result); mUploadMessage = null; } } } else if (resultCode == 10) { // Intent i = new Intent(context, FarmerTechnologyActivity.class); // setResult(10, i); // finish();  } } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @SuppressWarnings("null") private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) { if (requestCode != FILECHOOSER_RESULTCODE  || mUploadCallbackAboveL == null) { return; } Uri[] results = null; if (resultCode == Activity.RESULT_OK) { if (data == null) { results = new Uri[]{imageUri}; } else { String dataString = data.getDataString(); ClipData clipData = data.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)}; } } if (results != null) { mUploadCallbackAboveL.onReceiveValue(results); mUploadCallbackAboveL = null; } else { results = new Uri[]{imageUri}; mUploadCallbackAboveL.onReceiveValue(results); mUploadCallbackAboveL = null; } return; }