Crop Intent doesn't work properly in Android - android

Here is my intent code for select picture and cropping from gallery.
int PICK_IMAGE_REQUEST = 100;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 150);
intent.putExtra("aspectY", 150);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
getActivity().startActivityForResult(Intent.createChooser(intent,
"Complete using with."), PICK_IMAGE_REQUEST);
Here is my onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
int PICK_IMAGE_REQUEST = 100;
Bundle extras = null;
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
extras = data.getExtras();
}
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ImageView profilePhoto = (ImageView) findViewById(R.id.profileImageView);
profilePhoto.setImageBitmap(photo);
}
These codes above crop&set image successfully. However, sometimes it doesn't work properly. I mean when I'm using 3rd party gallery app instead of using device's default gallery app. It doesn't set the image. This may be not getting file path correctly when using another gallery app. So , how can I implement select&crop and set image into imageview ? I researched on to internet but nothing solved this problem so far.

Here is my intent code for select picture and cropping from gallery.
No, that is code for selecting a picture. The various extras that you have on there are not part of the ACTION_PICK documentation, or any other official documentation, for that matter.
These codes above crop&set image successfully
Not generally.
However, sometimes it doesn't work properly. I mean when I'm using 3rd party gallery app instead of using device's default gallery app.
There are thousands of Android device models. There is no single "default gallery app" for all of them; there will be dozens, if not hundreds, of "default gallery app" implementations. None have to support the random extras that you are trying. Also, none have to return something in a data extra, as ACTION_PICK returns a Uri in the result Intent, as is covered in the documentation for ACTION_PICK.
So , how can I implement select&crop and set image into imageview ?
Get rid of the extras. Get rid of the extras.getParcelable("data") bit. Get the Uri of the picked image (data.getData()). Use that in conjunction with one of various image cropping libraries available for Android.

Related

Android : Get photo from gallery always returns bitmap not uri

I call:
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("scale", true);
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("return-data", true);
startActivityForResult(intent, code);
then in my ActiviyResult():
if (requestCode == RESULT_LOAD_IMAGE && data != null) {
final Bundle extras = data.getExtras();
if (extras != null) {
//Get image
Bitmap profilePicBitmap = extras.getParcelable("data");
...
I try to get the uri by doing this:
data.getData()
but it always returns null
The problem is I need the path of the bitmap on the device so I can upload it elsewhere...but I can't get the path of it without having to convert it to uri. To convert to uri you have to request permission to write external storage, see here: (http://stackoverflow.com/questions/12555420/how-to-get-a-uri-object-from-bitmap) and I'd like to avoid that.
but it always returns null
That behavior will depend on the device and its apps.
I suspect that you will have better luck if you get rid of those undocumented extras. Presumably, rather than use an image-cropping library, you are trying to have the ACTION_PICK activity do the cropping. There are two problems with this:
There is no requirement for the ACTION_PICK activity to do any cropping, as those extras are not part of the Android SDK.
My guess is that since the cropped image does not exist as a file, they return it via the "data" extra (which is also outside the documented behavior of ACTION_PICK)
So, get rid of those extras. If you want to crop the image, use an image-cropping library.

Android data.getData() returns null from CameraActivity for some phones

I have a fatal error occurring in my onActivityResult coming back from a camera activity. What has me scratching my head is that the error is only happening on a handful of phones (based on the number of affected users) while there seems to be nothing wrong for the majority. I can duplicate the error on my Nexus 6 (running Lollipop 5.1.1) while my Note 5 (also 5.1.1) has no problems at all.
The problem is when I am trying to assign the imageUri from data.getData(). Debugging on the Note 5, data.mData equals "content://media/external/images/media/2215" while on the Nexus 6, data.mData is null.
I know this is a common question asked on SO but I haven't found anything that has helped me so far. Can anyone point me to the solution for this and provide an answer?
Method Starting Camera Activity for Result
#OnClick(R.id.change_image_camera) public void takePicture(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);}
onActvityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri imageUri = data.getData(); //The trouble is here
String realPath = Image.getPath(this, imageUri); //getPath crashes because imageUri is null
Image.compressImage(realPath);
File file = new File(realPath);
Bundle extra = new Bundle();
extra.putString("URL", realPath);
returnIntent.putExtras(extra);
setResult(RESULT_OK, returnIntent);
finish();
}
}
I greatly appreciate any help on this one!
Uri imageUri = data.getData(); //The trouble is here
There is no requirement for a camera app to return a Uri pointing to the photo, as that is not part of the ACTION_IMAGE_CAPTURE Intent protocol. Either:
Supply EXTRA_OUTPUT in your ACTION_IMAGE_CAPTURE Intent, in which case you know where the image should be stored, and do not need to rely upon getData(), or
Use the data extra in the response Intent, which will be a Bitmap of a thumbnail of the image
Your next bug is here:
String realPath = Image.getPath(this, imageUri);
There is no requirement that the image be stored as a file that you can access, unless you provide a path to that location via EXTRA_OUTPUT.
if(resultCode == RESULT_OK){
Bitmap bitmap = (Bitmap) imageReturnedIntent.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}

Choose a picture from the gallery only, not other application like Photos App

I am trying to pick an image from the Android Gallery only, not other applications like Photos, file manager etc
I need a solution to open the Gallery App directly, or is it possible to use the Photos Application to pick image?
1) Choose from gallery
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent,CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
2) onActivityResult result code
try {
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger images
options.inSampleSize = 2;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);
descimage.setImageBitmap(bitmap);
bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(new File(fileUri.getPath())));
photostatus = 1;
pbar.setVisibility(View.VISIBLE);
txtbrowser.setEnabled(false);
new upload().execute();
} catch (NullPointerException e) {
e.printStackTrace();
}
You should be aware that Gallery no longer exists on some devices running Lollipop. The photos app is the replacement and it should have no problems handling the intent to select an image. Intent.ACTION_GET_CONTENT is usually recommended for selecting images, such as:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, ID);
Opening the gallery on devices that do have it installed is discussed here. Basically every different vendor may ship a different gallery app.
It is possible to launch a specific activity for an implicit intent (such as selecting an image) without showing the chooser dialog by using the PackageManager.queryIntentActivities() API to iterate all the available packages on the users device so you can explicitly launch the one you require.
This intent allows you to pick the image from the default gallery.
// in onCreate or any event where your want the user to select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
For receiving the selected image in onActivityResult()
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
i got the solution from here

Losing a lot of quality on image crop

I'm capturing an image from camera and then saving it do sqlite database.
After that I allow user to crop it. After the whole process quality is very very poor.
I'm testing it on Nexus7 and I know it's front camera is poor but right after crop app opens, the picture is very small. It takes like 1/5 of s creen in the crop activity and I don't know why.
This what happens onActivityResult (capturing taken picture)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
String path = Images.Media.insertImage(getActivity().getContentResolver(), bitmap, "Title", null);
Uri imageUri = Uri.parse(path);
if (!doCrop(imageUri)) saveNewAvatar(bitmap);
}
}
And here is doCrop(Uri uri) method:
private void doCrop(final Uri imageUri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getActivity().getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
intent.setData(imageUri);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROP);
}
}
After the whole process picture is very very small.
EDIT:
Did some research and followed code from this thread
Ended up with this:
There are many wrong (as in: maybe works, but that's not the way you do such things on Android) things about your code, but the source of the problem is that you read the image from the data key in extras. The version in extras is just a miniature, and it is of very low quality.
What you need to do is change the way you invoke the camera. This is slightly counter-intuitive, but you do not get the url to the picture taken from the camera, but just the opposite: pass an url to the camera and it will try and put the picture there.
The url might point to the SD card or to you own ContentProvider.

Android camera intent file into imageview NPE (SGS3)

There's lots of questions on the same topic, however none of them provided me an answer.
I've tried a lot of solutions, yet none of them is working for me so far.
I'm invoking the camera intent in a fragment and inserting uri to the newly created file where I want the picture to be stored and then on activityresult I'm passing the uri to a new activity where I want to show it in an imageview and then proceed to upload it.
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File mImageFile = new File(Environment.getExternalStorageDirectory().getPath()+"/DCIM/" + "Camera/" + File.separator+System.currentTimeMillis()+".jpg");
imageUri = Uri.fromFile(mImageFile);
Log.d("camera", "imageUri: " + imageUri.toString());
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent, CAMERACODE);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case CAMERACODE:{
if (resultCode == Activity.RESULT_OK) {
Intent intent = new Intent(getActivity(), SubmitActivity.class);
intent.putExtra("imageuri", imageUri.toString());
getActivity().startActivity(intent);
and in SubmitActivity:
Bundle extras = getIntent().getExtras();
imageUri = extras.getString("imageuri"); <-- imageUri has the correct path, and the actual file is created and the photo is there. The photo is not showing up on gallery for some reason though, only when browsing the folders. What is the reason for this?
iv = (ImageView) findViewById(R.id.selectedpicture);
iv.setImageURI(Uri.parse(imageUri)); <-- this line causes a NullPointerException.
Can anyone explain the reason for why the setImageURI fails? Any alternative way of doing this? I'm using Samsung Galaxy S3
I believe iv is null and there is no problem with imageuri.
Check whether you have done setContentView and check if the layout has selectedpicture imageView.
Edit:
The photo is not showing up on gallery for some reason though
Gallery uses MediaStore to get all the photos on the sdcard. But when you take a picture MediaStore db would not have been updated. For the pic to show up in gallery MediaScanning should be run. Check this post

Categories

Resources