【问题标题】:Crop an Image by passing the image file path in Android通过在 Android 中传递图像文件路径来裁剪图像
【发布时间】:2013-08-30 01:30:28
【问题描述】:

我已经尝试了下面的代码。但是,它总是产生 160*160 尺寸的图像。

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

}

我想通过传递路径来裁剪图像。 我不想从默认相机应用程序或图库中捕获/选择。请帮我解决这个问题。

【问题讨论】:

    标签: android image crop


    【解决方案1】:

    这样就可以缩放图片了:

    Bitmap.createScaledBitmap(bitmap,50,50,true);
    

    【讨论】:

    • 我必须根据用户选择裁剪图像并发布答案。
    • @HarshParikh :实际上还有一个用于此目的的 SO 徽章 (stackoverflow.com/help/badges/1/teacher),这是一种鼓励行为。
    • 为什么你对我投了反对票。因此,我写了这一行
    【解决方案2】:

    我通过在调用 Intent 之前创建一个新文件并传递此文件路径以通过 Intent 存储裁剪的图像来解决此问题。这是解决此问题的方法。

    private Uri mCropImagedUri;
    private final int CROP_IMAGE = 100;//unique request code number. Which is used to identify the request result in onActivityResult()
    /**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()方法中的数据。

    这里是关于裁剪图像的完整信息。我已经用这个来解决了。 http://www.androidworks.com/crop_large_photos_with_android

    【讨论】:

    • 如果这是您的首选解决方案,请将其标记为此类并关闭问题
    • 经过几天的努力终于奏效了,tnx.多么愚蠢的错误! >:O
    • 这里的“mFinalImageUri”是指什么?
    【解决方案3】:

    Android 不支持内置的裁剪 Intent。您不应假设它可用。

    您应该使用自己的解决方案,或者使用第三方库,例如:

    https://github.com/ArthurHub/Android-Image-Cropper

    在此处了解更多信息:

    https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html

    【讨论】:

      猜你喜欢
      • 2015-02-02
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      相关资源
      最近更新 更多