Crop an Image by passing the image file path in Android

前端 未结 3 771
遥遥无期
遥遥无期 2020-12-03 12:38

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 ma         


        
相关标签:
3条回答
  • 2020-12-03 13:06

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

    So here we should not bother about the data which comes in onActivityResult() method.

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

    0 讨论(0)
  • 2020-12-03 13:08

    Android doesn't support a cropping intent built in. You should not assume it's available.

    Instead you should use your own solution, or use a third party library, like this one:

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

    Read more about it here:

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

    0 讨论(0)
  • 2020-12-03 13:18

    In this way,you can scale the image:

    Bitmap.createScaledBitmap(bitmap,50,50,true);
    
    0 讨论(0)
提交回复
热议问题