How to get image path Uri and Crop for all devices - android

I have my main project which gets images from certain sources (returns Uri). The next step was to crop the image to scale (touch input). I recently found out that some phone manufacturer mess around with the android base classes so:
com.android.camera.action.crop
doesn't always exist.
So I've found a library that does cropping: https://github.com/lvillani/android-cropimage
I've added the library into my eclipse build path and project library's.
my question is, can I open the library like so:
Intent intent = new Intent("com.android.camera.action.CROP");
//intent.setClassName("com.android.camera", "com.android.camera.CropImage");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("output", Uri);
intent.putExtra("outputFormat", "JPEG");
startActivityForResult(intent, 1);
And then retrieve the cropped image or do I have to do something else extra?
Another question: Also will this effect my app when I put it onto market (will the user need to download or accept permission for the extra library?
I want to make sure that my app works on everything, so is this the best way of doing it? Or is there better methods, please explain. (Also keep it simple, fairly new to Android dev!, thanks!)

I installed the library and made sure it was linking to my project and was in the build path, then I simply did:
Open file manager:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_FROM_FILE);
Alternatively if you want to capture from camera
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("return-data", true);
startActivityForResult(intent, GET_IMAGE_FROM_CAMERA);
Then crop image:
Intent intent = new Intent(this, com.android.camera.CropImage.class);
intent.setData(uri);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP);
I've tested this on a few devices and it's working fine and dandy. Also works with camera.

Related

How to start the "set as" intent (wallpaper, contact picture, etc)

I searched over the web during the last few weeks (seriously) but I can't find what I need. I just would like to start an intent corresponding to the set as action. It generally offers either Set as wallpaper or Set as contact picture. And then, if more application are installed on the device, they can be listed as well.
Here is an example of what I want :
I precise that I need to support API level 14 and higher.
I found getCropAndSetWallpaperIntent but it works only with content URI which is a problem for me, and is only availbable on API lvl 19 and higher.
I found the answer by my self :
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("jpg", "image/*");
startActivityForResult(Intent.createChooser(intent,
getString(R.string.set_as)), REQUEST_ID_SET_AS_WALLPAPER);
You just have to ensure that the uri is public and will be reachable by the crop application chosen by the user.
This solution worked for me with Uri:
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(contentUri, "image/*");
intent.putExtra("mimeType", "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(
intent, "Set as:"));
This worked for me :
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
//can't use normal URI, because it requires the Uri from file
intent.setDataAndType(Uri.fromFile(new File(uriOfImage)),"image/*");
intent.putExtra("mimeType","image/*");
startActivity(Intent.createChooser(intent,"Set Image"));
You can check that the URI that you pass, should contain the 'file://' prefix (It doesn't work without that).

How to pick multiple files from Android's gallery?

i need to open gallery and pick 1-n images OR 1-n videos (2 different intent) from Android's gallery, like the ACTION_PICK intent. How can I achieve that? There are some cool library on GitHub for multiple pick or custom gallery?
If you use API 18 or higher, you can add to your intent.putExtra like so:
Intent intent = new Intent();
intent.setType("image/*,video/*"); //For choosing both images and/or videos
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); //This should allow multiple selection
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);
There is also a GitHub project to build your own GridView with multi selection
Read here
To pick multiple files from gallery you need to simply modify your intent. For example:
Intent i = new Intent();
i.setType("image/*");
i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent,"Select"), 1);
Please note the i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

Android, Pick & Crop image using single startActivityForResult, but retrieving original image URI?

some time ago I managed to make the following code work (thanks to people on the Internet like you, definitely NOT the android documentation...).
What it basically does is start the file pick activity (in this case an image) and then jump right to the image crop activity, it then saves the cropped image to the provided file stream.
File file = new File(saveTo);
FileOutputStream fs = new FileOutputStream(file);
fs.close();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", maxImageSize);
intent.putExtra("outputY", maxImageSize);
intent.putExtra("scale", "true");
//intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", !false);
intent.putExtra("setWallpaper", false);
startActivityForResult(intent, CROP_FROM_CAMERA);
First of all, I'd like to make it clear that this works perfectly and I'm not having any problems with this code.
The above code doesn't return any data in its intent. This is because "return-data" extra is commented out by purpose, so that the cropped image is saved directly to the open stream instead of hogging device memory and to prevent ugly behavior with large images.
What I'd like to know is:
How can I get the URI of the ORIGINAL file (not the created file\thumbnail) that was chosen and then cropped?
Is there a way of doing this without taking apart working code and trying to do this in several steps? (Like first picking the image and then running the cropping activity, storing the original file URI in between)
After some research, I found out that it's bad practice to rely on this intent, since it might be removed or changed in future versions of the Android OS.
Guess that's why it was so hard to find any real information about it in the first place.
Anyway, I ended up using a custom crop library instead of relying on the internal android intent.

Pick Image From Android Device's Gallery App

In my android application, I implemented a feature to pick an image from Gallery. For that earlier I was doing this-
Intent pickImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
pickImageIntent.setType("image/*");
startActivityForResult(pickImageIntent, GALLERY_REQUEST_CODE);
By doing this, a dialog with all available image source application like Dropbox including Native Gallery app were displayed and it asks to choose one. Then I changed to
Intent pickImageIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickImageIntent.setType("image/*");
startActivityForResult(pickImageIntent, GALLERY_REQUEST_CODE);
By doing this most of applications were not diplayed but still some like Picasa are diplayed. I want to pick an image from Device's native gallery app only or we can say from either device's internal or external memory not from any third party application.
If any one has solution for this please help me with that.
To pick image from Gallery only
Intent pickImageIntent = new Intent(Intent.ACTION_PICK);
pickImageIntent.setType("image/*");
startActivityForResult(pickImageIntent, GALLERY_REQUEST_CODE);

Android Gallery always returns RESULT_CANCELED to onActivityResult

First of all, this is NOT the frequently-posted problem where the result code is returned prematurely. In this case, it is returned only after an item is picked in the gallery.
In my test case I call the Gallery with this code:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, SELECT_PICTURE);
and in onActivityResult there is:
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
pathText.setText(selectedImageUri.getPath());
}
} else {
pathText.setText("Result not OK - code is " + resultCode);
}
pathText is just a TextView I put in to show the result in the test case. In the actual application there is a different use for the path.
If I use ACTION_PICK instead of _GET_CONTENT I get the immediate failure reported by others. There are no launchMode tags in the manifest (some posts have suggested problems in that area).
Maybe there is a clue here. On a Toshiba Thrive, this bug does not appear, using Gallery, File Manager, or Fish Bowl Photo Gallery. On Kindle Fire, Quickoffice is also able to return an image path correctly. The bug only appears for me on the Kindle's built-in Gallery. The bug was also observed on a "Motorola Droid(2.3.4) , HTC EVO (2.3)".
Please, how can I get an image path back from the Gallery in a way that works on all these devices?
I was having same problem in one of my activities when I set launchMode="singleInstance" in manifest for that activity. It works fine when I remove that attribute.
I think, by default gallery does not return result code if you not specify in the intent to return result code. You can specify in the intent to return result code by adding this snippet in your code like this:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true); //added snippet
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_PICTURE);
Hi Steve try this seems to work on my wallpaper project
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_PICTURE);

Categories

Resources