Square crop an image from image picker Activiy - android

I have two methods that pick pictures for me. I want to do something like the Facebook profile picture square crop after choosing the picture. And was wandering if there is easy way like putting extras to the intent ("crop", "true"), ("aspectX/Y",1) and such. Right now I am experimenting with the intent extras, but can't make it work.
Constants.TAKE_CAMERA_PICTURE is 1000
Constants.SELECT_PICTURE_ACTIVITY_REQUEST_CODE is 1001
private void takeCameraPhoto() {
mPhotoHelper = PhotoHelper.recycle(mPhotoHelper);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoUri = Uri.fromFile(mPhotoHelper.getPhotoFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, Constants.TAKE_CAMERA_PICTURE);
}
public void importPhotoAlbum() {
mPhotoHelper = PhotoHelper.recycle(mPhotoHelper);
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, Constants.SELECT_PICTURE_ACTIVITY_REQUEST_CODE);
}
Thanks in advance!

I'm using jdamcd/android-crop library for this purpose. Just add this in your project as library..
Now to crop image just write one line i-e
Crop.of(source, dest).asSquare().start(this, CropCode);
here,
source is Uri of image file, dest is destination uri where image to be saved
It will do all the work for you.. Also there are other ways for cropping. Let me know if there is any difficulty for you :-)

If you want to use pure Android SDK , use the Bitmap class with the createBitmap method , check the docs Here , and check this example :
Bitmap croppedImage = Bitmap.createBitmap(sourceImage, startX, startY, destinationWidth, destinationHeight);
If you want to use Third party SDK, Take a look at this library Cropper Android widget for cropping and rotating an image ,i think it will do what you want ,check their wiki page with the following example :
1- add the CropImageView to your Layout XML file
<com.edmodo.cropper.CropImageView
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/CropImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
2- you can modify attributes programmatically by using provided CropImageView methods:
CropImageView cropImageView = (CropImageView) findViewById(R.id.CropImageView);
cropImageView.setAspectRatio(5, 10);
cropImageView.setFixedAspectRatio(true);
cropImageView.setGuidelines(1);
3- To retrieve the image contained within the Cropper window, use the provided method getCroppedImage() to retrieve a Bitmap of the cropped image.
croppedImage = cropImageView.getCroppedImage();
ImageView croppedImageView = (ImageView) findViewById(R.id.croppedImageView);
croppedImageView.setImageBitmap(croppedImage);
Hope That Helps.

Related

How to to take a picture from camera and gallery and then crop it without a library in Android?

I want to take a picture and then crop it but i don't want to use a library for that, so i would like to know if there is a way to do it with any Intent
I know how to take a photo from gallery someone can add camera
if you take a photo from gallery and you will show it in your image view:
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,2)
and you will override onactivityresult 2 is a number for request code you can use a integer variable instead of this
https://stackoverflow.com/a/31382240/15805169
I saw this but it uses Picasso library you have to implement it with this code
implementation 'com.squareup.picasso:picasso:2.71828'

android set home wallpaper using intent from drawable

I'm kind of new in android developing and i want to set home wallpaper with drawable image .
i wrote this code for setting but don't work and in visual android device at first i have dialog but next when i click as background i get this message :
"you need to insert sd card before using camera ..."
also i need use my drawable as source of images ...
here is my code :
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
MimeTypeMap map = MimeTypeMap.getSingleton();
String mimeType = map.getMimeTypeFromExtension("jpg");
Uri uri = Uri.parse("#drawable/wall_7");
intent.setDataAndType(uri,"image/jpeg" );
intent.putExtra("mimeType", mimeType);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
this.startActivityForResult(Intent.createChooser(intent,"Set as"),200);
and i going to have a slidable wallpaper , not a fix image . i'm using this intent bcs it let user crop image .
First of all you need to add this permession to your manifest.xml
<uses-permission android:name="android.permission.SET_WALLPAPER" />
Then you can use this to set it using the wallpaperManager :
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MyActivity.this);
Drawable drawable = getResources().getDrawable(R.drawable.wallpaper_img);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
wallpaperManager.setBitmap(bitmap);
Or you can use this
to set the wallpaper using intent
You can directly set background in your xml file using android:background tag.Let me know if you have any further queries.

Bitmap not setting to ImageView [duplicate]

I know the basics on how to take a picture and set it to ImageView.
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
I want to do a little more than that.
I am saving it to a folder on to an SD card. That I have done successfully with this:
// intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAMERA_REQUEST);
Here is my next question:
Not sure how to do this: What I'd like to do next: The next time I come to this Activity, I'd like to check if that image exists and assign it to that imageView.
Last days i faced this issue in one of my applications.
I'll try here to explain a little bitte what i have done.
Try to save the picture full path to a storage area or to your sharedpreferences.
Next time if you call your activity then check if a picture already exists and if you can use it.
Prepare in your xml layout an ImageView with visibility="gone" and if the point (2.) is true then you can change the visibility to visible and set the image in the view.
If the point (2.) is false then switch to camera view (SurfaceView) in order to take a new picture
You would have to use some sort of persistent storage to reference the file to check if it exists. I would just store it as a string in a preference and read it, then check if it exists and so on. Easy enough to do from the onCreate().

Saving Camera Bitmap to Storage, and Setting Image with Bitmap

I know the basics on how to take a picture and set it to ImageView.
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
I want to do a little more than that.
I am saving it to a folder on to an SD card. That I have done successfully with this:
// intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAMERA_REQUEST);
Here is my next question:
Not sure how to do this: What I'd like to do next: The next time I come to this Activity, I'd like to check if that image exists and assign it to that imageView.
Last days i faced this issue in one of my applications.
I'll try here to explain a little bitte what i have done.
Try to save the picture full path to a storage area or to your sharedpreferences.
Next time if you call your activity then check if a picture already exists and if you can use it.
Prepare in your xml layout an ImageView with visibility="gone" and if the point (2.) is true then you can change the visibility to visible and set the image in the view.
If the point (2.) is false then switch to camera view (SurfaceView) in order to take a new picture
You would have to use some sort of persistent storage to reference the file to check if it exists. I would just store it as a string in a preference and read it, then check if it exists and so on. Easy enough to do from the onCreate().

Android camera (getting imageView from a different class)

I'm currently working on a app which the user can answer a question through images by taking photos or uploading photos from the album.
I've separated into two classes which are FulfillPhotoTaskActivity, AddPhotoActivity.
FulfillPhotoTaskActivity have imageView, addphoto button and save button.
when I press addphoto button, it goes to AddPhotoActivity which we can select two options(taking photos or uploading photos). I finished the part where if the user press take photo button then it opens the camera and take the photo. I created onActivityResult which get the image data. but in many example, inside the onActivityResult they also have imageView, like setImageBitmap.
My problem is, how can I get the imageView from the AddPhotoActivity and shows it in FulfillPhotoTaskActivity which I already made for XML.
When you take a photo it saves the image to a file. You can retrieve this image file and then you can call your imageView in FulfillPhotoTaskActivity and set the imageView to the image that is in the file.
The following code will start the camera intent and the file to a given location:
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder/SubFolder");
String folder = root.toString();
File file = new File(folder, "fileName" + ".jpg");
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 0);
Now when the user takes a picture you know where it will be saved. In your FulfillPhotoTaskActivity you can call your imageview:
ImageView imageView = (ImageView)findViewById(R.id.YOUR_IMAGE_VIEW_ID);
Finally, you can just set the image to the imageview:
imageView.setImageBitmap(BitmapFactory.decodeFile(file));
So, you don't need to get the imageview from AddPhotoActivity. You only need the file name. Then, you can go back to FulfillPhotoTaskActivity and set the imageview to the bitmap file.
I hope this helps!

Categories

Resources