image not cropping after cropped - android

I am attempting to crop an image using intent after the image is selected from the gallery. Here is my snippet of code
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
//******code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
Here I am calling the above snippet with PICK_IMAGE_REQUEST intent handle
#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) {
try {
Uri filePath = data.getData();
what could be wrong since I am using the same intent after cropping which is PICK_IMAGE_REQUEST

There are no documented and supported "crop" extras for ACTION_GET_CONTENT or any other standard Android Intent. Nor is there a documented and supported standard Intent action for cropping. There is no requirement for any device to have apps that support undocumented and unsupported extras, actions, etc.
If you want to crop an image, use any one of a number of existing open source libraries for image cropping.

Related

How Can I Get the Uri of Image if I Open the Gallery and Select Image and share with my App

How Can I Get the Uri of Image if I Open the Gallery and Select Image and share with my App, but Image is not showing in ImageView
Open gallery for getting Image Uri
private static final int REQUEST_SELECT_IMAGE = 100;
private void handleOpenGallery() {
Intent intentSelect = new Intent(Intent.ACTION_GET_CONTENT);
intentSelect.setType("image/*");
startActivityForResult(Intent.createChooser(intentSelect, getString(R.string.select_picture)),
REQUEST_SELECT_IMAGE);
}
Uri of selected Image
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SELECT_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
shareImage(uri);
}
}
}
Share Image
private void shareImage(Uri imageUri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, "Share to"));
}
You can follow below code by Raghu to open gallery,
if you only want to open gallery then you can use,
Intent intentSelect = new Intent(Intent.ACTION_PICK);
Add check for read/write permissions if OS version is above Marshmallow(6.0)
If you still don't get the uri then in onActivityResult when you will fetch the uri try the getPath method from here
Add check if the Android OS version is above Nougat(7.0) or not, as above nougat while fetching file uri you may get "FileUriExposedException" refer this link for more info

Intents for File Picker

I can find some very old file-picker samples, but think it might now be possible to do it with the more up to date Intent.
I want to load and save files. It will be a text file but with an extension .mfl (an extension I am using for my files).
I have seen someone's code for images (but just for loading):
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
What do I need in setType to only see my .mfl files?
I already have a onActivityResult doing other stuff, so need to create my own requestCode. I was thinking
int PICK_MYFILE_REQUEST = 1001;
So I think my onActivityResult needs to look like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(/* doing my other stuff */){
} else if (requestCode == PICK_MYFILE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
/* load the file */
} catch (IOException e) {
e.printStackTrace();
}
}
}
As you can see I think I might be almost there, but not completely sure what I am meant to be doing. (I am new to Android - I think you can guess.)
Is this roughly right? What do I need to do to get this working?
Also, how do I modify this to get a file saver dialog?
Try this :
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
For custom type you can use application/mfl so your Intent should be:
//Use ACTION_OPEN_DOCUMENT to get read/write permission
Intent intent = new Intent(Intent.Intent.ACTION_OPEN_DOCUMENT);
//"application/mfl" to request only .mfl files
intent.setType("application/mfl");
//Request readable files
intent.addCategory(Intent.CATEGORY_OPENABLE)
startActivityForResult(Intent.createChooser(intent, "Select File"),PICK_MYFILE_REQUEST);

How to open an Image without saving another Location?

now file:// uri are not supported. people say to use FileProvider but i think i have to save image in app data location to use this. is there a way to get content:// URI Without Saving image in data directory.i want to open image in sd card from gallery. this below code works on old phones, after android 7 it is not working. FileUriExposedException example image file is in dcim folder my old code is this
File pic2toview = new File(imagepath);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(pic2toview), "image/*");
startActivity(i);
Code to select an image from gallery by intent,
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE_REQUEST);
Then it handled in onActivityResult,
#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) {
imageUrl = data.getData();
}
}
Here imageUrl is Uri of image selected from gallery.

Take Picture with Camera then Crop

I want to be able to take a picture with the the camera. I do it this way and it works:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAMERA_REQUEST);
After this is successful, I want user to be able to IMMEDIATELY view image and be able to crop.
I can do that like this:
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(Uri.fromFile(new File(file.toString())), "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CAMERA_REQUEST);
How do I merge these two tasks so they happen one after the other? Do I have to have TWO startActivityForResult? Should it be merged? Or should the croppin ginfo be inside the normal?
getActivity();
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
// Cropping code here? another intent?
iPP.setImageBitmap(BitmapFactory.decodeFile(file.toString()));
imagePath = file.toString();
scaleImage();
new UploadImage().execute();
}
Create a new constant called CAMERA_CROP.
When you start the activity after taking a photo, send the CAMERA_CROP request code.
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
...
startActivityForResult(cropIntent, CAMERA_CROP);
}
When you return from the Crop Activity, handle the request code.
if (requestCode == CAMERA_CROP && resultCode == Activity.RESULT_OK) {
...
}

how to get the images from device in android java application

In my application I want to upload the image.
For that I have to get images from gallery in android device.
How do I write code that accomplishes this?
Raise an Intent with Action as ACTION_GET_CONTENT and set the type to "image/*". This will start the photo picker Activity. When the user selects an image, you can use the onActivityResult callback to get the results.
Something like:
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri chosenImageUri = data.getData();
Bitmap mBitmap = null;
mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri);
}
}

Categories

Resources