【问题标题】:Android intent with multiple option i.e , Pick image from gallary and capture image using front camera具有多个选项的 Android 意图,即从图库中选择图像并使用前置摄像头捕获图像
【发布时间】:2015-04-09 07:56:28
【问题描述】:

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

我已经完成了这个。但问题是当我切换到 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);
}

谁能帮我举个例子吗?

【问题讨论】:

    标签: java android


    【解决方案1】:

    我用这种方法裁剪了一张也在 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);
            }
    
        }
    

    【讨论】:

    • 我认为这应该写在 OnActivityResult 中。我们不能像我上面的代码一样设置裁剪和画廊意图吗?感谢您的支持:)
    • Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, 1);检查我的活动结果编辑
    • Karthika :) 我的意思是这个代码只有在调用画廊/相机意图后才能使用。我不想通过从 onactivty 结果调用来这样做。我的问题是我们可以像这样设置 Intent pickIntent =新意图(); pickIntent.setType("图片/jpeg"); pickIntent.setAction(Intent.ACTION_GET_CONTENT); pickIntent.putExtra("crop", "true");
    • @AnoopM 当我使用它时,我从我的画廊中获得了一个包含图像的布局,然后单击其中一个图像,设备的cropimage 窗口将打开你想要除此之外的功能吗?跨度>
    • 在查找功能而不是编码方式时很好..我似乎最好使用外部库来摆脱应用程序崩溃..谢谢你的时间:)
    【解决方案2】:

    就是这样..

       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);
    }
    

    所有功能都适用于 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多