MarshmallowCrop Sample
public class Imgmarsh extends Activity {
public static final int RequestPermissionCode = 1;
@Bind(R.id.imgSelImage)
ImageView ivSelectImage;
@Bind(R.id.btn_Camera)
Button btnCamera;
@Bind(R.id.btn_Gallery)
Button btnGallary;
File file;
Uri uri;
Intent intent_Camera, intent_Gallary, intent_Cropping;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagemarsh);
ButterKnife.bind(this);
Check_EnableRuntimePermission();
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GetFromCamera();
}
});
btnGallary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GetFromGallary();
}
});
}
public void GetFromCamera() {
intent_Camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(),
"file" + String.valueOf(System.currentTimeMillis()) + ".jpg");
uri = Uri.fromFile(file);
intent_Camera.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
intent_Camera.putExtra("return-data", true);
startActivityForResult(intent_Camera, 0);
}
public void GetFromGallary() {
intent_Gallary = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent_Gallary, "Get Image From Gallery"), 2);
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
CropSelectedImage();
} else if (requestCode == 2) {
if (data != null) {
uri = data.getData();
//Cropping image
CropSelectedImage();
}
} else if (requestCode == 1) {
if (data != null) {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
ivSelectImage.setImageBitmap(bitmap);
}
}
}
public void CropSelectedImage() {
// Cropping image started here
try {
intent_Cropping = new Intent("com.android.camera.action.CROP");
intent_Cropping.setDataAndType(uri, "image/*");
intent_Cropping.putExtra("crop", "true");
intent_Cropping.putExtra("outputX", 180);
intent_Cropping.putExtra("outputY", 180);
intent_Cropping.putExtra("aspectX", 3);
intent_Cropping.putExtra("aspectY", 4);
intent_Cropping.putExtra("scaleUpIfNeeded", true);
intent_Cropping.putExtra("return-data", true);
startActivityForResult(intent_Cropping, 1);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
public void Check_EnableRuntimePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(Imgmarsh.this,
Manifest.permission.CAMERA)) {
Toast.makeText(Imgmarsh.this, "Permission for access camera",
Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(Imgmarsh.this, new String[]{
Manifest.permission.CAMERA}, RequestPermissionCode);
}
}
@Override
public void onRequestPermissionsResult(int reqCode, String per[], int[] PrmsnResult) {
switch (reqCode) {
case RequestPermissionCode:
if (PrmsnResult.length > 0 && PrmsnResult[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(Imgmarsh.this, "Successfully camera permission granted",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Imgmarsh.this, "Camera permission canceled",
Toast.LENGTH_SHORT).show();
}
break;
}
}
}
//compile 'com.jakewharton:butterknife:7.0.1'
No comments :
Post a Comment