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.
Related
I have the codes work for loading images from the gallery but I really do not understand how it works. Here are the codes.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { //Browse Gallery is requested
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();
loadImage(picturePath); //load picture according the path
image_View.setImageBitmap(pic); //Show the selected picture
}
}
Uri selectedImage = data.getData();
Get the uri of selected image from intent
String[] filePathColumn = { MediaStore.Images.Media.DATA };
MediaStore.Images.Media.DATA is constant. I do not understand why do not use String instead of String[]
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
I do not understand this line.
cursor.moveToFirst();
Move to the first picture in Gallery.
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
For this one, I always get 0 no matter which picture I choose.
String picturePath = cursor.getString(columnIndex);
Since columnIndex is always 0, then how can it get a different path for different pictures?
Can anyone help me to check whether my explanation is correct and explain the line that I do not understand? Thanks.
1-
Uri selectedImage = data.getData();
This is the statement where you need to read the data passed through another intent which you called earlier via startActivityForResult method. In this case probably you open an intent and let the user select an image, then the URI of the image will be returned to you and you use getData to read that.
2-
String[] filePathColumn = { MediaStore.Images.Media.DATA };
When you want cursors to read something a Content Provider (via ContentResolver) you need to specify which columns you need to read from the database, and the argument you need to pass should be an array of String (whether it has one or more columns you still need to pass an array). MediaStore.Images.Media is a Database Contract which contains constants which you need to use to talk to Content Providers
3-
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
Cursors are used for reading data from Content Providers. If you are familiar with other programming languages it is like reading rows from database and your results are stored in Cursors. When you pass URI, you don't need to specify which database to read, ContentResolver will find that out for you (this is an advantage of using content providers)
4-
cursor.moveToFirst();
When you read the desired rows from the database (in this case probably you just selected one image), you need to move the cursor to point to the first entry (row) of the returned results
5-
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
You need to know which column you need to access to read your desired data (in this case file path name). so you ask cursor what is the column index of the file pathname and it will return you the column index. and of course in this case it will be always 0 because you only asked the content provider to return one column (file pathname) so there would be no more data to show other than that
6-
String picturePath = cursor.getString(columnIndex);
and finally this statement asks cursor to get the file pathname located at the index (in this case index 0) so at the end you have your path to file. Note that you can read only one picture data at a time with this method
Cursors store query result records in rows and grant many methods to access and iterate through the records. Also int columnIndex = cursor.getColumnIndex(filePathColumn[0]); is the reason why you always get 0 for your index
In my project i need a way of get the path of several images selecteds from the gallery.
I´m using this library MultipleSelectImages
It´s apparently work fine, but in the onActivityResult i need the array with the path of each image, however the result I get is this:
Paths: [com.darsh.multipleimageselect.models.Image#1e6d3057, com.darsh.multipleimageselect.models.Image#33824744]
...when i need the real path (/storage/emulated/0/DCIM/Camera/20150426_110936.jpg)
Reading the doc of the library don´t found solution.
This is the onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
//The array list has the image paths of the selected images
ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
Log.i("myLogs", "Paths:" + " " + images);
}
}
...Where "image" it´s imported from the library
import com.darsh.multipleimageselect.models.Image;
I´m not using EXTRA_ALLOW_MULTIPLE because need that the app works in android api 16 version
Thanks in advanced.
Try to use below code;
onActivityResult();-
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 imgPath = cursor.getString(columnIndex);
cursor.close();
I've developed image cropper so that user can crop selected image from gallery. When user has taken a picture with camera or has selected it from gallery, image uri is queried for image metadata and image file path is passed to various bitmap methods for bitmap creation.
But now I've expanded users selection option to Dropbox also and uri that I receive from intent is in the form of:
01-08 09:11:21.051: I/DROPBOX(2761): file:///storage/emulated/0/Android/data/com.dropbox.android/files/u98667695/scratch/Capture.PNG
Query on this uri returns null for cursor, so I tried to convert uri to string, remove first 7 characters and pass that string as image file path. This works fine if I select image from root directory on Dropbox...
So, my question how image request from external sources like Dropbox for instance in order to get image file path when intent returns uri for requested image?
This is method where I'm retrieving file path:
private void getImageData(Uri selectedImage){
String[] filePathColumn = { MediaStore.Images.Media.DATA };
String[] fileOrientationColumn = { MediaStore.Images.Media.ORIENTATION };
Cursor cursor = context.getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
/* if(cursor == null){
imageFilePath = selectedImage.toString().substring(6);
return;
}
*/ cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageFilePath = cursor.getString(columnIndex);
Log.i("CURSOR path", imageFilePath);
cursor = context.getContentResolver().query(selectedImage,
fileOrientationColumn, null, null, null);
cursor.moveToFirst();
columnIndex = cursor.getColumnIndex(fileOrientationColumn[0]);
String fileOrientation = cursor.getString(columnIndex);
// Izmena
if(fileOrientation == null)fileOrientation = "0";
imageOrientation = Integer.parseInt(fileOrientation);
cursor.close();
Log.i("CURSOR orientation", fileOrientation);
}
In above case you can do the following.
String filePath="I/DROPBOX(2761): file:///storage/emulated/0/Android/data/com.dropbox.android/files/u98667695/scratch/Capture.PNG";
int firstIndex=filePath.indexOf("file");
String actualPath=filePath.subString(firstIndex);
So the actualPath contains required path as follows, irrespective of root directory or file location.
actualPath="file:///storage/emulated/0/Android/data/com.dropbox.android/files/u98667695/scratch/Capture.PNG"
I want take image from camera then save image in SD card folder but path is save in SqLite database and also save image in binary format. Also when read from database then show image in ImageView or GridView. any have resource or example or tutorial links about this. If anybody give me any resource, or example or tutorial links. It will be very helpful for me.
You can split your task in three independent parts:
1. Take a picture from camera
2. Save it to a file and take the path
3. Store the path from point two in your database
For part 1 and 2:
final int IMAGE_FROM_CAMERA = 0;
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, IMAGE_FROM_CAMERA);
In onActivityResult()...
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String path = getRealPathFromURI(selectedImage);
}
...
public String getRealPathFromURI(Uri contentUri) {
String path = null;
String[] proj = { MediaStore.MediaColumns.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null,
null, null);
if (cursor.moveToFirst()) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
path = cursor.getString(column_index);
}
cursor.close();
return path;
}
For part 3 just write the string path the appropriate field in your database. For opening is easy, since you have the path, just open a stream from the path, do some conversions and get your image.
I am running to the problem with using Droid X's Files app and Astro file manager to select an image file. This two apps return the selected image with the scheme "file://" while Gallery returns the image with the scheme "content://". How do I convert the first schema to the second. Or how do I decode the image with the second format?
You probably want to convert content:// to file://
For gallery images, try something like this:
Uri myFileUri;
Cursor cursor = context.getContentResolver().query(uri,new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
if(cursor.moveToFirst())
{
myFileUri = Uri.parse(cursor.getString(0)).getPath();
}
cursor.close
Here, the problem is that, for all files we can't have a content Uri (content://). Because content uri is for those files which are part of MediaStore. Eg: images, audio & video.
However, for supported files, we can find its absolute path. Like for images as follows-
File myimageFile = new File(path);
Uri content_uri=getImageContentUri(this,myimageFile);
The generic method is as follows.
public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}}
Use ContentResolver.openInputStream() or related methods to access the byte stream. You shouldn't generally be worrying about whether it is a file: or content: URI.
http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.Uri)