标签:android blog java 使用 os io 文件 for
把这两个功能封装起来,经常使用到。
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 == null) return null; // 读取相册缩放图片 if (requestCode == PHOTOZOOM) { photoUri = data.getData(); startPhotoZoom(activity,data.getData()); } // 处理结果 if (requestCode == PHOTORESULT) { Bundle extras = data.getExtras(); if (extras != null) { //photoUri = data.getData(); String picPath = null; String[] pojo = {MediaStore.Images.Media.DATA}; Cursor cursor = activity.managedQuery(photoUri, pojo, null, null,null); if(cursor != null ) { int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]); cursor.moveToFirst(); picPath = cursor.getString(columnIndex); //cursor.close(); } Log.d("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; }
android 调用相册-照相机封装,布布扣,bubuko.com
标签:android blog java 使用 os io 文件 for
原文地址:http://www.cnblogs.com/gfqFighting/p/3900703.html