I want to save only the cropped image into the gallery. but it saves the original full image. how to save only the cropped image?

前端 未结 4 1425
有刺的猬
有刺的猬 2021-01-16 19:00
public class ImageCroppMainActivity extends Activity implements OnClickListener{

final int CAMERA_CAPTURE = 1;

//keep track of cropping intent

final int PIC_CROP          


        
4条回答
  •  甜味超标
    2021-01-16 19:12

    HI please try this to save to your gallery
    MainActivity:
    -----------------
    
    package com.example.satis.crop;
    
    import android.Manifest;
    import android.annotation.SuppressLint;
    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.content.ActivityNotFoundException;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.drawable.BitmapDrawable;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.support.v4.content.FileProvider;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.webkit.WebView;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    import com.theartofdev.edmodo.cropper.CropImage;
    import com.theartofdev.edmodo.cropper.CropImageView;
    
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.List;
    import java.util.Random;
    
    import pub.devrel.easypermissions.AfterPermissionGranted;
    import pub.devrel.easypermissions.AppSettingsDialog;
    import pub.devrel.easypermissions.EasyPermissions;
    
    import android.Manifest;
    import android.content.ActivityNotFoundException;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Environment;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.Toast;
    import java.io.File;
    
    public class MainActivity extends AppCompatActivity {
    
        ImageView imageView;
        Button buttonCamera, buttonGallery ;
        File file;
        Uri uri;
        Intent CamIntent, GalIntent, CropIntent ;
        public  static final int RequestPermissionCode  = 1 ;
        DisplayMetrics displayMetrics ;
        int width, height;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            imageView = (ImageView)findViewById(R.id.imageview);
            buttonCamera = (Button)findViewById(R.id.button2);
            buttonGallery = (Button)findViewById(R.id.button1);
    
            EnableRuntimePermission();
    
            buttonCamera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    ClickImageFromCamera() ;
    
                }
            });
    
            buttonGallery.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    GetImageFromGallery();
    
                }
            });
    
        }
    
        public void ClickImageFromCamera() {
    
            CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    
            file = new File(Environment.getExternalStorageDirectory(),
                    "file" + String.valueOf(System.currentTimeMillis()) + ".jpg");
            uri = Uri.fromFile(file);
    
            CamIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
    
            CamIntent.putExtra("return-data", true);
    
            startActivityForResult(CamIntent, 0);
    
        }
    
        public void GetImageFromGallery(){
    
            GalIntent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    
            startActivityForResult(Intent.createChooser(GalIntent, "Select Image From Gallery"), 2);
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == 0 && resultCode == RESULT_OK) {
    
                ImageCropFunction();
    
            }
            else if (requestCode == 2) {
    
                if (data != null) {
    
                    uri = data.getData();
    
                    ImageCropFunction();
    
                }
            }
            else if (requestCode == 1) {
    
                if (data != null) {
    
                    Bundle bundle = data.getExtras();
    
                    Bitmap bitmap = bundle.getParcelable("data");
    
                    imageView.setImageBitmap(bitmap);
                    MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "cropped" , "cropped");
    
    
                }
            }
        }
    
        public void ImageCropFunction() {
    
            // Image Crop Code
            try {
                CropIntent = new Intent("com.android.camera.action.CROP");
    
                CropIntent.setDataAndType(uri, "image/*");
    
                CropIntent.putExtra("crop", "true");
                CropIntent.putExtra("outputX", 180);
                CropIntent.putExtra("outputY", 180);
                CropIntent.putExtra("aspectX", 3);
                CropIntent.putExtra("aspectY", 4);
                CropIntent.putExtra("scaleUpIfNeeded", true);
                CropIntent.putExtra("return-data", true);
    
                startActivityForResult(CropIntent, 1);
    
            } catch (ActivityNotFoundException e) {
    
            }
        }
        //Image Crop Code End Here
    
        public void EnableRuntimePermission(){
    
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                    Manifest.permission.CAMERA))
            {
    
                Toast.makeText(MainActivity.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();
    
            } else {
    
                ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                        Manifest.permission.CAMERA}, RequestPermissionCode);
    
            }
        }
    
        @Override
        public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {
    
            switch (RC) {
    
                case RequestPermissionCode:
    
                    if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {
    
                        Toast.makeText(MainActivity.this,"Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();
    
                    } else {
    
                        Toast.makeText(MainActivity.this,"Permission Canceled, Now your application cannot access CAMERA.", Toast.LENGTH_LONG).show();
    
                    }
                    break;
            }
        }
    
    }
    
    
    AndroidManifest.xml
    -----------------------
    
    
    
    
        
        
        
    
        
            
                
                    
    
                    
                
            
        
    
    
    
    
    Activity_main:
    -------------
    
    
    
        
    
        

提交回复
热议问题