通过将图像文件路径的Andr​​oid裁剪图像 [英] Crop an Image by passing the image file path in Android

查看:132
本文介绍了通过将图像文件路径的Andr​​oid裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试以下code。然而,它总是导致160 * 160尺寸的图像。

I have tried the below code. However, it always results the 160*160 dimension image.

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(Uri.fromFile(pictureFile), "image/*");
    //set crop properties
    cropIntent.putExtra("crop", "true");
    //indicate aspect of desired crop
    cropIntent.putExtra("aspectX", 100);
    cropIntent.putExtra("aspectY", 100);
    cropIntent.putExtra("scale", true);

    //indicate output X and Y
    cropIntent.putExtra("outputX", 500);
    cropIntent.putExtra("outputY", 500);
    //retrieve data on return
    cropIntent.putExtra("return-data", true);
    //start the activity - we handle returning in onActivityResult
    startActivityForResult(cropIntent, CROP_IMAGE);

} catch(ActivityNotFoundException anfe) {
    //display an error message
    String errorMessage = "Whoops - your device doesn't support the crop action!";
    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
    toast.show();

}

我想传递的路径,裁剪图像。 我并不想捕捉/选择从默认的摄像头应用程序或画廊。请帮我在这。

I would like to crop an image by passing its path. I don't want to capture/pick from default camera app or gallery. Please help me on this.

推荐答案

我已经解决了这个被创建调用意向,并把该文件的路径,通过意图存储裁剪后的图像前,一个新的文件。下面是这种情况的解决方案。

I have solved this by create a new file before calling Intent and passing this file path to store the cropped image through intent. Here is the solution for this.

private Uri mCropImagedUri;
/**Crop the image
 * @return returns <tt>true</tt> if crop supports by the device,otherwise false*/
private boolean performCropImage(){
    try {
        if(mFinalImageUri!=null){
            //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(mFinalImageUri, "image/*");
            //set crop properties
            cropIntent.putExtra("crop", "true");
            //indicate aspect of desired crop
            cropIntent.putExtra("aspectX", 1);
            cropIntent.putExtra("aspectY", 1);
            cropIntent.putExtra("scale", true);
            //indicate output X and Y
            cropIntent.putExtra("outputX", 500);
            cropIntent.putExtra("outputY", 500);
            //retrieve data on return
            cropIntent.putExtra("return-data", false);

            File f = createNewFile("CROP_");
            try {
                f.createNewFile();
            } catch (IOException ex) {
                VLLog.e("io", ex.getMessage());  
            }

            mCropImagedUri = Uri.fromFile(f);
            cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCropImagedUri);
            //start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, CROP_IMAGE);
            return true;
        }
    }
    catch(ActivityNotFoundException anfe){
        //display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
        return false;
    }
    return false;
}

private File createNewFile(String prefix){
    if(prefix==null || "".equalsIgnoreCase(prefix)){
        prefix="IMG_";
    }
    File newDirectory = new File(Environment.getExternalStorageDirectory()+"/mypics/");
    if(!newDirectory.exists()){
        if(newDirectory.mkdir()){
            VLLog.d(mContext.getClass().getName(), newDirectory.getAbsolutePath()+" directory created");
        }
    }
    File file = new File(newDirectory,(prefix+System.currentTimeMillis()+".jpg"));
    if(file.exists()){
        //this wont be executed
        file.delete();
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return file;
}

所以在这里,我们不应该理会它进来 onActivityResult数据()方法。

下面是关于裁剪图像的完整信息。我用这个来解决。 <一href="http://www.androidworks.com/crop_large_photos_with_android">http://www.androidworks.com/crop_large_photos_with_android

Here is the complete information about cropping image. I have used this to solve. http://www.androidworks.com/crop_large_photos_with_android

这篇关于通过将图像文件路径的Andr​​oid裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆