频道栏目
首页 > 程序开发 > 移动开发 > Android > 正文
android 开发 web页面调取相册相机 上传图片
2018-02-12 09:43:15      个评论    来源:徐玮的博客  
收藏   我要投稿

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; }
点击复制链接 与好友分享!回本站首页
上一篇:android开发之音视频(二)
下一篇:android 输入系统 映射文件kl
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站