good day. I am hoping that anyone can answer this. I will be straight to my question.
How can I get ALL the image location of the selected images from image gallery?
For example if I select three images on the gallery, it should output me like this.
[/storage/extSdCard/DCIM/Camera/image1.jpg, /storage/extSdCard/DCIM/Camera/image2.jpg, /storage/extSdCard/DCIM/Camera/image3.jpg]
This is my code to laod the Image Gallery
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"), 5);
and this is my code to get the image link
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
imgPath = cursor.getString(columnIndex);
Log.i("Path : ",imgPath);
and this is my log
11254-11254/com.example.com I/Path : /storage/extSdCard/DCIM/Camera/20170104_171334.jpg
As you can see only one were return, even I have been selected 3 items
IMHO Why you don't using RESTful? If you will use JSON + Retrofit, it will be easy way to add something new client-server api, isn't it?
Related
in my app I use SqLite Database "Templates", where I have table "favourites", constains of four columns "id", "description", "imagePath", "category". I would like user to be able use their own images in my app, like this:
When user click button, he choose image from gallery, write description and choose category of image. I know I need use Content Values to do this. But how can I do my table has imagePathes, i.e how can I show them later using RecyclerView and Picasso?
You can load image from device memory:
Picasso.get().load(new File("path-to-you-image/image.png")).into(imageView);
If you are asking "how to get image path" - you can get it in the onActivityResult method:
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
I would like to pick photos for my app via the Android gallery, however, I only want to be able to pick ones with certain Exif Attributes e.g taken in the last x hours
Is there a way to call the Android gallery via an intent but filter what the user can see/select?
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMG_RESULT);
you can use the cursor for that
String[] columns = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.DATE_TAKEN};
String orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC";
cursor = getActivity().managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
columns,
null,
null,
orderBy);
I'm using an image picker intent, to allow users to choose an image from their gallery, I get it's path, and pass it then to a 3rd library.
It's working fine for most of the cases, but if I picked up an image from Google Photos (an image that is stored online) I get a null path, though that I get valid URI for both of the working and not working images.
Here is my Intent call:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
and here is onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
Log.e(getClass().getName(),"file uri = " + uri);
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(uri, projection,
null, null, null);
if(cursor == null) return;
Log.e(getClass().getName(),"file cursor = " + cursor);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.e(getClass().getName(),"file columnIndex = " + columnIndex);
cursor.moveToFirst();
// The crash happens here
String photoPath = cursor.getString(columnIndex);
Log.e(getClass().getName(),"file photo path = " + photoPath);
cursor.close();
cropImage(photoPath);
}
And here are the logs for working and not-working image:
Working image:
file uri = content://com.google.android.apps.photos.contentprovider/0/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F105681/ORIGINAL/NONE/187859359
file cursor =
android.content.ContentResolver$CursorWrapperInner#8953964
file columnIndex = 0
file photo path = /storage/emulated/0/DCIM/Camera/IMG_20190523_184830.jpg
Not working image:
file uri =
content://com.google.android.apps.photos.contentprovider/0/1/mediakey%3A%2Flocal%253A4574915c-b4ac-40af-bc08-b1004670cab2/ORIGINAL/NONE/477302338
file cursor =
android.content.ContentResolver$CursorWrapperInner#59448a4
file columnIndex = 0
file photo path = null
If there isn't way to avoid that error, is there a way instead to hide photos that are stored online and only show local ones?
The technique in your question has (at least) three problems:
Not every MediaStore entry has a value for DATA, as you're seeing
Not every non-null DATA value represents a filesystem path that you can access, as the MediaStore can get to content that you can't
The DATA column is not available on Android Q and higher
In your case, the uCrop library accepts a Uri. Well-written Android libraries know how to handle a Uri, so you can just hand the Uri over to the library and it will take it from there.
I am trying to retrieve the images from Mediastore.Images.Media I am always getting the cursor to be null. Please suggest how to solve the issue. Thanks in advance
Uri contenturi = Images.Media.getContentUri("phoneStorage");
String[] mprojection = { MediaStore.Images.Media.DATA };
String mselection = null;
String[] mselectionArgs = null;
String msortOrder ="date_added DESC";
Cursor mcursor = managedQuery(contenturi, mprojection, mselection, mselectionArgs, msortOrder);
When I am checking it says the mcursor to be null, I have tried changing the uri to external storage also tried with no sort order. But still its returning null. Please help
buddy try this code,
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,getResources().getString(R.string.accept)),1);
Now just use onActivityResult method to get image from Gallery.
I am trying to pick a picture from the gallery and set it as a background for the view. Now, I am able to select pics as long as they are on my SD card. But as my gallery is in sync with my picasa album and when I try to select one of those pics, the cursor is returning NULL. Below is my code.
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
The selected image has URI of "
https://lh5.googleusercontent.com/-thIdOA38IO0/SY8GG8PqW4I/AAAAAAAABKA/f3XpPvY9JHo/s1024/Picture%252520007.jpg"
Can someone help?
Thx!
Rahul.
Uri selectedImage = Uri.parse(imageReturnedIntent.getDataString());
Try the above line which return the Uri of selectedImage.