I'm trying to let the user:
Click on an ImageView
Pick an Image
Crop the Image
Finally display the cropped Image in the ImageView
So far it kinda works, but I've got a few issues:
After choosing the Image, the User gets asked AGAIN which program he wants to use for cropping. I don't want that
The cropping intent displays the area-to-crop as a circle. I'd rather have a square!
Thanks for any help guys, here's the complete class:
public class settings extends Activity {
ImageView masterUserImg;
private static int LOAD_IMAGE_RESULTS = 1;
final int PIC_CROP = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.settings);
masterUserImg=(ImageView)findViewById(R.id.masterUserImage);
masterUserImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Create the Intent for Image Gallery.
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.
startActivityForResult(i, LOAD_IMAGE_RESULTS);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri pickedImage = null;
// 1) PICK IMAGE
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
pickedImage = data.getData();
}
// 2) CROP IMAGE
performCrop(pickedImage);
// 3) LOAD IMAGE
if (requestCode == PIC_CROP) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
masterUserImg.setImageBitmap(selectedBitmap);
}
}
}
private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
catch (Exception ex) {
// display an error message
String errorMessage = "Crop failed";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
Please try this Cropper Library . It will fulfill your requirement.
It supports features which you needed .
For not prompt activity for cropping, you can try query activities for CROP intent and use one of them. For querying activities available for certain intent, please consult the document.
For the circle shape of crop area, it's the implementation of the activity which is handling the crop intent, there's no way to change that if you don't known how to configure it with correct extras parameters.
But as #CommonsWare said, there's no com.android.camera.action.CROP intent, there will be phones have no activity to handle that.
So you should use a crop library to handle that, like android-crop.
Finally, for your requirement, you can try droid-image-picker which let you select image from gallery and camera, and also integrate android crop for the cropping part.
Disclosure: I'm the developer of droid image picker
Related
These codes can only capture and display image I took, and when I pressed save button, it only saves "Note title" onto my list View. And when I clicked "Note Title" it doesn't display the picture anymore. What should I add to save it and update it? Thanks.
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
imageviewid.setImageBitmap(mphoto);
}
}
public void takeImageFromCamera(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
private void saveNote(){
fillNameIfEmpty();
id = DbAccess.addNote(getBaseContext(), etName.getText().toString(), mFileName, DbContract.NoteEntry.TYPE_PHOTO, currentCat);
Toast.makeText(getApplicationContext(), R.string.toast_saved, Toast.LENGTH_SHORT).show();
}
private void updateNote(){
fillNameIfEmpty();
DbAccess.updateNote(getBaseContext(), id, etName.getText().toString(), mFileName, currentCat);
Toast.makeText(getApplicationContext(), R.string.toast_updated, Toast.LENGTH_SHORT).show();
}
Your DbAccess API doesn't know anything about an image in the note!
So I would extend your code to:
After picture is taken it saved in fail in application sandbox
Save filename of image assigned to note to database together with note
And don't forget to handle editing or deletion picture use cases!
I could select an image from gallery in android. But, i want to select next and previous images of the selected image. How do i do it?
This works fine.. for selecting and then displaying the image..now what do i do for selecting next and previous images?
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
// Log.d(TAG, String.valueOf(bitmap));
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I could select an image from gallery in android
Your code happens to bring up a gallery app on your device. You are requesting ACTION_GET_CONTENT for image/*. This means:
Other apps, besides a gallery, can respond to that Intent and can be chosen by the user
There will be devices that do not have an app that would be considered a "gallery"
How do i do it?
Write your own gallery. There is no concept of "next" or "previous" with ACTION_GET_CONTENT.
There are many image picker libraries for Android that might form the basis for your own gallery UI.
I forgot to write the most important thing, which is code for previous and next is not working.. till now i have written code only for previous, coz once it works; next ll be just few changes ahead.
moveToprevious is coming false n cursor is declared as global.. so that when a query for cusor s fired in getcontentresolver() is fired, it gets the needed value..
Please look into this!
I am using the SurfaceHolder.Callback and Camera.PictureCallback to open Camera in my activity After taking image i want to crop that image and then save it to sd card. I found many solution for cropping via intent camera but didn't find any solution as per my Requirement.
I'll be thankful if someone help me.
Thanks in Advance.
You should implement Camera.PreviewCallback instead of Camera.PictureCallback,
by using the interface implemented method:
public void onPreviewFrame(byte[] data, Camera camera) {
if (shutterEnabled){
shutterEnabled = false;
// Process data
// Put it on a Bitmap
// Send it to cropImage
// -----
}
}
Take in account that if you didn't set on your camera parameters, setPreviewFormat, the picture on viewPreviewFrame would arrive as YUV (NV21) format.
Actually I'm looking how to make this part, this is why I bumped into your question.
The point here, how are you going to pass the picture to the crop method, you should look into that.
The shutterEnabled flag, it's enabled from your listener that 'watches' for that event.
private void cropImage( picture){
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picture, "image/*");
cropIntent.putExtra("aspectX",0);
cropIntent.putExtra("aspectY",0);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CAMERA_CROP_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK && requestCode == CAMERA_CROP_CODE){
Bitmap bmp = (Bitmap)data.getExtras().get("data");
}
}
I am working on android application. I have an Activity in which there is two button first one for selecting image from gallary. i have applied function on it. i have one more button capture image . i want to work on it .but don't know how to start camera .I want that when i click button capture image it should start camera for capture image.and there should be option to cancel if don't want to take picture. after pressing cancel camera should cancel.
if i captures image it should it should show in Image View and automatically store in SD card .how should i proceed.
http://developer.android.com/guide/topics/media/camera.html. Everything you need to know about starting a camera. Go through the link.
private static final int TAKE_PHOTO_CODE = 1;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) );
startActivityForResult(intent, TAKE_PHOTO_CODE);
get uri
private File getTempFile(Context context){
return new File(path, "/tourpath/yourfilename.jpg");
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode){
case TAKE_PHOTO_CODE:
try {
Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file));
iv.setImageBitmap(captureBmp);//show in imageview
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
in my app i m calling built in camera for capturing picture but i want to set that picture into an image view following is the code . so what code should i add to it to set the picture into imageview.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
String result = data.toURI();
// ...
}
}
Thanks in advance
Add a button and an ImageView to your main layout. When you click the button, first intent.putExtra to specify where the image will be stored, then start the camera intent. In your onActivityResult, if the resultCode comes back 0 that means the user accepted the picture. In this case, go grab the image from the path you specified as an extra of the intent. Create a bitmap from the file specified by the path, then set the image bitmap in your ImageView.