首页 > 代码库 > android 调用相册-照相机封装
android 调用相册-照相机封装
把这两个功能封装起来,经常使用到。
private static Uri photoUri; public static final int NONE = 0; public static final int PHOTOHRAPH = 1;// 拍照 public static final int PHOTOZOOM = 2; // 缩放 public static final int PHOTORESULT = 3;// 结果 public static final String IMAGE_UNSPECIFIED = "image/*"; /** * 进入照相机 * @param activity */ public static void startCamera(Activity activity) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE" ContentValues values = new ContentValues(); photoUri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri); /**-----------------*/ activity.startActivityForResult(intent, PHOTOHRAPH); } /** * 进入相册 * @param activity */ public static void startPhotoAlbum(Activity activity) { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); activity.startActivityForResult(intent, PHOTOZOOM); } /** * 裁剪照片 跳系统的activity * @param activity * @param uri */ public static void startPhotoZoom(Activity activity, Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, IMAGE_UNSPECIFIED); intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 300); // 决定图片的像素 intent.putExtra("outputY", 300); // 决定图片的像素 intent.putExtra("return-data", true); activity.startActivityForResult(intent, PHOTORESULT); } /** * 相册||照相 结果返回处理 放在Activity的onActivityResult里 * @param activity * @param requestCode * @param resultCode * @param data * @return */ public static Object[] handleResultFromCameraOrPhotos(Activity activity ,int requestCode, int resultCode, Intent data) { if (resultCode == NONE) return null; // 拍照 if (requestCode == PHOTOHRAPH) { AppContext applicationContext = (AppContext)activity.getApplicationContext(); String name="temp";//+applicationContext.setNum(); name="temp"; String path = Environment.getExternalStorageDirectory()+"/"+name+".jpg"; System.out.println(path); File picture = new File(path); photoUri = data.getData(); startPhotoZoom(activity,photoUri); } if (data =http://www.mamicode.com/= null) "TAG", picPath +""); Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);// (0 - 100)压缩文件 Object[] hashMap = new Object[2]; hashMap[1] = photo; hashMap[0] = picPath; //photo.recycle(); return hashMap; } } return null; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。