三九宝宝网宝宝绘画儿童画

android自定义相机如何拍照上传图片到webview

02月12日 编辑 39baobao.com

private void take(){ // 调用相机前开启服务 mServiceIntent = new Intent(mContext, HcService.class); startService(mServiceIntent); runOnUiThread(new Runnable() { Override public void run() { File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp"); FileUtil.deleteDir(imageStorageDir); // Create the storage directory if it does not exist if (!imageStorageDir.exists()){ if (!imageStorageDir.mkdirs()) return; } File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); //设定图片的uri路径 imageUri = Uri.fromFile(file); final ListcameraIntents = new ArrayList(); //调用相机的intent final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); //获取包管理器(就是包含了整个清单文件,其中包括application,activity) final PackageManager packageManager = getPackageManager(); //查询相机intent的activity,ResolveInfo其实就是activity节点 final ListlistCam = packageManager.queryIntentActivities(captureIntent, 0); //进行遍历 for(ResolveInfo res : listCam) { //获取list中的元素,就activity,就是根据activity拿到相机的包名 final String packageName = res.activityInfo.packageName; //将相机的intent 赋给新的intent final Intent i = new Intent(captureIntent); //重新设置当前intent的Component (写全包名) i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); i.setPackage(packageName); i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); cameraIntents.add(i); } //这个是文档内容,包含image,音视频 Intent i = new Intent(Intent.ACTION_GET_CONTENT); //添加分类 i.addCategory(Intent.CATEGORY_OPENABLE); //类型为图片 i.setType("image/*"); //开始创建跳转应用的对话框,可以自己设置dialog样式,也可以像下面这样,同时创建包含多个 Intent chooserIntent = Intent.createChooser(i,"Image Chooser"); //添加额外初始相机intent,但要序列化 chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{})); mContext.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); } }); }

推荐阅读
图文推荐