I'm facing a problem that I cannot solve. I would be grateful if you could help me.
I would like to make an app that picks one or more images from the gallery, elaborates them and saves them into a folder.
The problem I have is to rotate the images correctly.
When I pick one image (see the code below), the app rotates the image correctly.
If I pick more images simultaneously (see the code below) I cannot rotate them because the value of "rotation" (see the code below) is always 0 (zero).
To pick one image from the gallery, I use the following code:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
In "OnActivityResult" I get the URI path as following:
Uri imageUri = data.getData();
To pick more images I use the following code (that doesn’t open the gallery, just the recent images, but it’s acceptable for me):
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);
In "OnActivityResult" I get the URI path as following:
ClipData mClipData = data.getClipData();
and in a "for":
ClipData.Item item = mClipData.getItemAt(i);
Uri imageUri = item.getUri();
To get the image orientation I use the following code:
String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
Cursor cur = getApplicationContext().getContentResolver().query(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Note: I tried to use the “ExifInterface” class but it always returns 0 (zero) on my Samsung S7 Android 7.0.
I noticed that the method to pick one image returns a URI path like “content://media/external/images/media/imageX”, instead the method to pick more images returns a URI path like “content://com.android.providers.media.documents/document/imageX”. I don’t know if it can be the problem.
Thank you everybody in advance.
Vincenzo
Related
I am using below code and it works fine below android 5. I am able to pick image or video from SD card.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/* image/*");
getActivity().startActivityForResult(photoPickerIntent, 1);
However on Android L it shows only videos.
tried searching but didn't find anything, any help will be appreciated.
hi #Mohit you can use this solution for image and video
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("*/*");
getActivity().startActivityForResult(photoPickerIntent, 1);
for both image and video you can use setType(*/*);
here ACTION_GET_CONTENT is give only gallery selection while ACTION_PICK give many more options to pick image and video from different action, So as per #DipeshDhakal answer you should use only ACTION_GET_CONTENT.
and this is work on android L and api 10 also.
Use Intent.ACTION_GET_CONTENT
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("video/*, images/*");
startActivityForResult(photoPickerIntent, 1);
Was running into a similar issue. Code that worked on 5.0 and below started breaking on 5.1+, and only filtered by the first type passed in.
A co-worker came up the following, and I thought I'd share:
We were previously using the following intent:
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*,video/*");
and the following code to get the path from whatever the user selected, if anything:
public static String getFilePathFromURI(Uri imageUri, Activity context) {
String filePath = null;
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(imageUri,
filePathColumn, null, null, null);
if (cursor != null) {
try {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
} finally {
cursor.close();
}
}
return filePath;
}
Now:
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
String[] mimetypes = {"image/*", "video/*"};
i.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
And to get the path, the getPath function found in this answer:
https://stackoverflow.com/a/20559175/434400
When i pick some photos from gallery, they have next uri
content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh3.googleusercontent.com%2F1cSCAm2mqyFgOUPR8d_8Modz0Cgu6yn1nsznrhfzQTw%3Ds0-d
instead of something like
content://media/external/images/media/3728
and i cant get path to this image. How can i resolve this problem?
My code:
case RC_SELECT_PICTURE:
Uri pickedImage = data.getData();
// Let's read picked image path using content resolver
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
cursor.close();
break;
I try to get filePath using some methods which I found on StackOverflow, but this path is url. I can download my image using it. I need to get local filePath.
A Uri has never had to map to a File path.
Now, it turns out that MediaStore leaked paths, and a few idiots promoted the pattern of using those leaks, leading lots of developers to think that this was reliable behavior. It wasn't. A ContentProvider can serve up all sorts of streams, not backed by a file that you have access to, such as having the file be on internal storage.
If you want the content of the image pointed to by this Uri, or you want to know its MIME type, use a ContentResolver.
Try this may hepls you
Check Android SDK version when you go for pick image from gallery.
if (Build.VERSION.SDK_INT <19){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED);
} else {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);
}
How would I correctly get a buttons intent to take a picture and store that image in the phones gallery? So far i have a button which is in a case structure that says :
else if (v.getId() == R.id.button5)//camera
{
Intent c = new Intent( MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(c,1);
}
what should my onActivityResult look like since i am just storing that image to the gallery?
would i have to use something as bundle extras = data.getExtras();?
This answer may be your problems solution. But data.getExtras() returns null in some cases and unfortunately I have not detected all of these cases yet. For example data.getExtras() worked fine on android 2.3 HTC Evo 3D but on android 2.3 Samsung Galaxy SII, it returned null. Hope this helps.
First, you have to call your startActivityForResult() method like the following:
Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(i, CAMERA_RESULT);
And in the implementation of startActivityForResult() method you have to write the following:
// Save the name and description of an image in a ContentValues map.
ContentValues contentValues = new ContentValues(3);
contentValues.put(Media.DISPLAY_NAME, "This is a test title");
contentValues.put(Media.DESCRIPTION, "This is a test description");
contentValues.put(Media.MIME_TYPE, "image/jpeg");
// Add a new record without the bitmap, but with some values set.
// insert() returns the URI of the new record.
Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,contentValues);
I am launching the image capture intent to take a picture, adding a uri so the picture is tiny. My problem is I want to set the output jpeg-quality before I start the activity.
ContentValues vals = new ContentValues();
vals.put(Media.DISPLAY_NAME, "test title");
vals.put(Media.MIME_TYPE, "image/jpeg");
Uri imageFileUri = context.getContentResolver()
.insert(Media.EXTERNAL_CONTENT_URI, vals);
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
context.startActivityForResult(i, CameraImportActivity.CAMERA_REQUEST);
I suppose I could downsample after the picture is taken, but if I can request the activity do it for me I would like to do so.
As bonus questions, how do I change the album they get saved to in the gallery, and how do I prevent the camera activity from saving a copy in the default location (on my phone it is "photos")
I got all images from device's gallery,but i got images path is like content://media/external/image/media/102,so i want to get real image path for each images and send email,how can i get path?given below code i used,anybody knows,please give some sample code for me..i have email code.i want to only get real image path.How can I convert this path to real one (just like '/sdcard/image.png') ?
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
Uri photoUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor actualimagecursor = managedQuery(photoUri, proj,null, null, null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
it work well..