I have a code which takes images from gallery using gallery intent and data is send to my activity as an uri like:
content://media/external/images/media/338
I need to get the path of this picture and I am using
public String getRealPathFromURI(Uri contentUri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, projection, null, null, null);
try {
cursor.moveToFirst();
return cursor.getString(0);
} finally {
cursor.close();
}
}
but this code causes a crash on android ICS when trying to close a managed cursor. This is a known issue (see issue). Is there a way to close this cursor without causing a crash? I don't want to exit this method without closing the cursor. I need to suport android 2.1 and up. Thanks
found it:
public String getRealPathFromURI(Uri contentUri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, projection, null, null, null);
try {
cursor.moveToFirst();
return cursor.getString(0);
} finally {
cursor.close();
}
}
Related
I am trying to get the path of an image when the user pick from the gallery (with intent)
It's been working ok, since some users noticed that could not do it witn Android 6.0.
I have tried different things, and some solutions works in the emulator with Android 6.0 but not in my Xiamoi with Android 6.1.
This both solutions works in the emulator (6.0) and Android 4.4 physycal device.
public String getRealPathFromURI(Activity context, Uri contentURI) {
String[] projection = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = context.managedQuery(contentURI, projection, null,
null, null);
if (cursor == null)
return null;
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
String s = cursor.getString(column_index);
// cursor.close();
return s;
}
// cursor.close();
return null;
}
and the other similar:
private static String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
But in my Xiaomi 6.1 the cursor is null. But I can get the real path from:
private static String getRealPathFromURI(Context context, Uri contentUri) {
return contentUri.getEncodedPath();
}
Any help?
Thank you!
EDIT:
I'm asking for choose an image in this way:
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK); //ACTION_GET_CONTENT
// Always show the chooser (if there are multiple options available)
launchForResult(Intent.createChooser(intent, "Select Picture"), SELECT_FILE);
I'm asking for choose an image in this way:
First, use ACTION_GET_CONTENT to pick by MIME type.
Second, whatever activity that responds to ACTION_GET_CONTENT (or ACTION_PICK) does not need to return a Uri that the MediaStore knows about. In fact, most will not. They can return a Uri that points to anything, including things that are not files.
So, get rid of all of your broken getRealPathFromURI() code. Use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri, and use that stream.
I am new to android. I want get cursor to one file only. But how? I tried this but doesn't work and gives error.
private void getallaudioFromMySpecialFolder() {
String[] star = { "*" };
String Dir_recordedAudios= Environment.getExternalStorageDirectory()+File.separator+"My Audios"+File.separator;
File Directory=new File(Dir_recordedAudios);
Uri u=Uri.fromFile(Directory);
Uri uri = Uri.parse(u.toString());
Cursor musiccursor = managedQuery(uri, star,null, null, null);
if (musiccursor!= null) {
if (musiccursor.moveToFirst()) {
do {
String duration = musiccursor.getString(musiccursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String str_duration=CalTotalTime(Integer.valueOf(duration));
String path= musiccursor.getString(musiccursor.getColumnIndex(MediaStore.Audio.Media.DATA));
Log.d("************", "**************");
Log.d("path",path);
Log.d("totalTime", str_duration);
} while (musiccursor.moveToNext());
}
musiccursor.close();
}
}
I finally Find my question solving . If we want cursor to a special file,first in Projection array determine aour fileds for projection. And we must define aur file which want fetching its info.
We use of a statement with "LIKE" word for selection. Code is here. Too,I learned that using from CursorLoader is better.because managedQuery method is deprecated.
Code is here:
String name="";
String duration="";
String path="";
Log.d("Loading file: " + filepath,"");
// This returns us content://media/external/videos/media (or something like that)
// I pass in "external" because that's the MediaStore's name for the external
// storage on my device (the other possibility is "internal")
Uri audiosUri = MediaStore.Audio.Media.getContentUri("external");
Log.d("audiosUri = " + audiosUri.toString(),"");
String[] projection = {MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.DURATION};
// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = managedQuery(audiosUri, projection, MediaStore.Audio.Media.DATA + " LIKE ?", new String[] { filepath }, null);
cursor.moveToFirst();
path =cursor.getString( cursor.getColumnIndex(projection[0]));
name=cursor.getString( cursor.getColumnIndex(projection[1]));
int iduration = cursor.getColumnIndex(projection[2]);
duration=CalTotalTime(Integer.valueOf(iduration));
Log.d("pathhhhhhhhhhhhhhhhhh", path);
Log.d("nameeeeeeeeeeeeeeeeeeeeeee", name);
Log.d("duration", duration);
cursor.close();
And this is a formal of CursorLoader .May be helps you.
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Audio.Media.DATA };
CursorLoader loader = new CursorLoader(mContext, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(proj[0]);
cursor.moveToFirst();
return cursor.getString(column_index);
}
I'm trying to retrieve the metadata from a video file (title, language, artist) using the method MediaStore.Video.query(). However, the method is always returning null. The code is bellow:
String[] columns = {
MediaStore.Video.VideoColumns._ID,
MediaStore.Video.VideoColumns.TITLE,
MediaStore.Video.VideoColumns.ARTIST
};
Cursor cursor = MediaStore.Video.query(getApplicationContext().getContentResolver(), videoUri,columns);
if (cursor != null) {
cursor.moveToNext();
}
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.TITLE));
Any suggestion about how to return video metadata using android?
==Update
As I searched in many places, I tried one solution using CursorLoader. However, the method loadInBackground() from CursorLoader is also returning null. The code is showed bellow:
String[] columns = {
MediaStore.Video.VideoColumns.TITLE
};
Uri videoUri = Uri.parse("content://mnt/sdcard/Movies/landscapes.mp4");
CursorLoader loader = new CursorLoader(getBaseContext(), videoUri, columns, null, null, null);
Cursor cursor = loader.loadInBackground();
cursor.moveToFirst();
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.TITLE));
Uri.parse("content://mnt/sdcard/Movies/landscapes.mp4") is not an Uri for MediaStore. It would try to find a ContentProvider for authority mnt which does not exist.
MediaStore can handle only content://media/... Uris which you should get exclusively via MediaStore, not by using Uri.parse().
In your case use the following for example
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] columns = {
MediaStore.Video.VideoColumns._ID,
MediaStore.Video.VideoColumns.TITLE,
MediaStore.Video.VideoColumns.ARTIST
};
String selection = MediaStore.Video.VideoColumns.DATA + "=?";
String selectionArgs[] = { "/mnt/sdcard/Movies/landscapes.mp4" };
Cursor cursor = context.getContentResolver().query(uri, columns, selection, selectionArgs, null);
The MediaStore.Video.VideoColumns.DATA field holds the path to the videos and you search for a certain video this way. At least for now, future versions of Android may change that.
Your second example is using CursorLoader the wrong way. If you call loader.loadInBackground() yourself, you load the data in foreground. See e.g. http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/
The next thing you do is
Cursor cursor = getCursor();
cursor.moveToFirst();
String title = cursor.getString(/* some index */);
This will lead to a CursorIndexOutOfBoundsException if your cursor has 0 rows and cursor.moveToFirst() failed because there is no first row. The cursor stays before the first row (at -1) and that index does not exist. That would mean in your case that the file was not found in the database.
To prevent that use the return value of moveToFirst - it will only be true if there is a first row.
Cursor cursor = getCursor(); // from somewhere
if (cursor.moveToFirst()) {
String title = cursor.getString(/* some index */);
}
A more complete example including checks for null and closing the cursor in all cases
Cursor cursor = getCursor(); // from somewhere
String title = "not found";
if (cursor != null) {
if (cursor.moveToFirst()) {
title = cursor.getString(/* some index */);
}
cursor.close();
}
I guess the file you try to find is either not indexed in the database (rebooting forces the indexer to run again) or the path is wrong.
Or the path you use is actually a symlink in which case MediaStore might use a different path.
Use this to get rid of symlinks
String path = "/mnt/sdcard/Movies/landscapes.mp4";
try {
path = new File(path).getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
Yes, I tested now and it is throwing IndexOutOfBoundsException. When I'm using cursor.getColumnCount() it returns 1
cursor.getColumnCount() is the column count, not the row count. It should always be the same as the number of columns you requested in columns. You need to check cursor.getCount() if you want to check the row count.
Try dumping all the videos known to MediaStore into logcat in case it does not show as expected.
public static void dumpVideos(Context context) {
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.VideoColumns.DATA };
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
int vidsCount = 0;
if (c != null) {
vidsCount = c.getCount();
while (c.moveToNext()) {
Log.d("VIDEO", c.getString(0));
}
c.close();
}
Log.d("VIDEO", "Total count of videos: " + vidsCount);
}
I updated your code, it works, just check it
public static void dumpVideos(Context context) {
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.VideoColumns.DATA };
Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
int vidsCount = 0;
if (c != null) {
c.moveToFirst();
vidsCount = c.getCount();
do {
Log.d("VIDEO", c.getString(0));
}while (c.moveToNext());
c.close();
}
Log.d("VIDEO", "Total count of videos: " + vidsCount);
}
after capture the image in android how to get a name that save in SD card to programme? or how to set a name for capturing image to save in SD card. such as using incremental number.
pls give me some sample code
I found answer from here.
using this method
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
I want to handle ACTION_SEND intent.
So i get an uri of the shared item using this code:
Bundle extras = intent.getExtras();
if (extras.containsKey(Intent.EXTRA_STREAM))
{
Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
....
this uri is something like this:
content://com.android.contacts/contacts/as_vcard/0n3B4537432F4531
How i can get exact contact from this uri?
I tried this:
Cursor cursor = managedQuery(uri, null, null, null, null);
and this:
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
and got an exception and program termination in both cases.
Please help!
Just remove as_vcard in content://com.android.contacts/contacts/as_vcard/0n3B4537432F4531
If it does not work try this.
String key= uri.getPathSegments().get(2);
Cursor cursor = getContentResolver().query(Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, key),
null, null, null, null);
If you receive a content:// URI in an EXTRA_STREAM you should be able to query OpenableColumns to get the meta information.
String name = null;
String size = null;
Cursor cursor = getContentResolver().query(uri, new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE }, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
name = cursor.getString(0);
size = cursor.getString(1);
}
} finally {
cursor.close();
}
}
To get the actual content use ContentResolver.openInputStream(Uri) and read the stream. Don't forget to close it afterwards.