具有多个选项的Android意图,即使用前置摄像头从gallary中选择图像并捕获图像

选择/捕获图像后,我需要裁剪图像。

我已经完成了这一个。但问题是当我切换到Android最新版本(kitkat)时,作物意图不能正常工作。

我的代码

private void picPhoto() { Intent pickIntent = new Intent(); if (Build.VERSION.SDK_INT < 19) { pickIntent.setType("image/jpeg"); pickIntent.setAction(Intent.ACTION_GET_CONTENT); pickIntent.putExtra("crop", "true"); } else { pickIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); pickIntent.addCategory(Intent.CATEGORY_OPENABLE); pickIntent.setType("image/jpeg"); pickIntent.putExtra("crop", "true"); } Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFileDirectory()); takePhotoIntent.putExtra("android.intent.extras.CAMERA_FACING", 1); String pickTitle = "Select or take a new Picture"; Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle); chooserIntent.putExtra ( Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePhotoIntent} ); startActivityForResult(chooserIntent, PICK_IMAGE); } 

任何人都可以帮我一个例子吗?

我用这种方法裁剪了一张在kitkat上工作的图片

 @Override public void onActivityResult(int requestCode, int resultCode, final Intent data) { if (resultCode == Activity.RESULT_OK&&requestCode==1) { performCrop(image_uri); } } private void performCrop(Uri picUri) { thePic = null; // take care of exceptions try { // call the standard crop action intent (the user device may not // support it) Intent cropIntent = new Intent("com.android.camera.action.CROP"); // indicate image type and Uri cropIntent.setDataAndType(picUri, "image/*"); // set crop properties cropIntent.putExtra("crop", "true"); // // indicate aspect of desired crop cropIntent.putExtra("aspectX", 2); cropIntent.putExtra("aspectY", 1); // // // indicate output X and Y // retrieve data on return cropIntent.putExtra("return-data", true); // start the activity - we handle returning in onActivityResult startActivityForResult(cropIntent, 3); } // respond to users whose devices do not support the crop action catch (ActivityNotFoundException anfe) { Toast toast = Toast.makeText(getActivity(), "This device doesn't support the crop action!", Toast.LENGTH_SHORT); toast.show(); } catch (OutOfMemoryError e) { System.out.println("out of memoryyy"); } catch (Exception e) { Display display = getActivity().getWindowManager() .getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; Intent cropIntent = new Intent("com.android.camera.action.CROP"); // indicate image type and Uri cropIntent.setDataAndType(picUri, "image/*"); // set crop properties cropIntent.putExtra("crop", "true"); // // indicate aspect of desired crop cropIntent.putExtra("aspectX", 2); cropIntent.putExtra("aspectY", 1); // // indicate output X and Y cropIntent.putExtra("outputX", width); cropIntent.putExtra("outputY", 200); // retrieve data on return cropIntent.putExtra("return-data", true); // start the activity - we handle returning in onActivityResult startActivityForResult(cropIntent, 3); } } 

所以这就是..

  private void picPhoto() { Intent pickIntent = new Intent(); if (Build.VERSION.SDK_INT < 19) { pickIntent.setType("image/jpeg"); pickIntent.setAction(Intent.ACTION_GET_CONTENT); } else { pickIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); pickIntent.addCategory(Intent.CATEGORY_OPENABLE); pickIntent.setType("image/jpeg"); } Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFileDirectory()); takePhotoIntent.putExtra("android.intent.extras.CAMERA_FACING", 1); String pickTitle = "Select or take a new Picture"; Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle); chooserIntent.putExtra ( Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePhotoIntent} ); startActivityForResult(chooserIntent, PICK_IMAGE); } 

所有function都将工作Api> 14即Android 4.0(ICE_CREAM_SANDWICH)。

  @TargetApi(Build.VERSION_CODES.KITKAT) @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == PICK_IMAGE && data != null && data.getData() != null) { Uri _uri = data.getData(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { String wholeID = DocumentsContract.getDocumentId(_uri); // Split at colon, use second item in the array String id = wholeID.split(":")[1]; String[] column = {MediaStore.Images.Media.DATA}; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = getActivity().getContentResolver(). query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{id}, null); String filePath = ""; int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); croppedPath = filePath; } else { //User had pick an image. Cursor cursor = getActivity().getContentResolver().query(_uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null); cursor.moveToFirst(); //Link to the image final String imageFilePath = cursor.getString(0); croppedPath = imageFilePath; cursor.close(); } if (croppedPath != null) { cropIntent(croppedPath, 5); } } else if (requestCode != 4 && requestCode == PICK_IMAGE && data == null) { cropIntent(getFileDirectory().getPath(), 4); } else if (requestCode == 4) { editPhoto1.setImageBitmap(null); if (bmp != null) { bmp.recycle(); } setImagePicked(getFileDirectory().getPath(), 0); } else if (requestCode == 5) { if (croppedPath != null) { editPhoto1.setImageBitmap(null); if (bmp != null) { bmp.recycle(); } setImagePicked(getFileDirectory().getPath(), 0); croppedPath = null; } } } super.onActivityResult(requestCode, resultCode, data); } 

裁剪方法

  private void cropIntent(String fileToCrop, int requestCode) { Uri filePath = Uri.fromFile(new File(fileToCrop)); Intent cropIntent = new Intent("com.android.camera.action.CROP"); //indicate image type and Uri cropIntent.setDataAndType(filePath, "image/*"); cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFileDirectory()); //set crop properties cropIntent.putExtra("crop", "true"); //indicate aspect of desired crop cropIntent.putExtra("aspectX", 1); cropIntent.putExtra("aspectY", 1); //indicate output X and Y cropIntent.putExtra("outputX", 256); cropIntent.putExtra("outputY", 256); //retrieve data on return cropIntent.putExtra("return-data", true); startActivityForResult(cropIntent, requestCode); //start the activity - we handle returning in onActivityResult }