How can I get a list of all videos and their path that can be found on an Android device where is my app installed?
EDIT:
I am trying to make a simple video player, so the idea would be to display all the videos that can be found on the device and to play it on list item click.
Only problem is that i don't know how to get that info about videos, so I would be very thankful if someone can help me.
If someone is still looking for answer please Try this method. This method will return list of path of all media.
public ArrayList<String> getAllMedia() {
HashSet<String> videoItemHashSet = new HashSet<>();
String[] projection = { MediaStore.Video.VideoColumns.DATA ,MediaStore.Video.Media.DISPLAY_NAME};
Cursor cursor = getContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
try {
cursor.moveToFirst();
do{
videoItemHashSet.add((cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))));
}while(cursor.moveToNext());
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
ArrayList<String> downloadedList = new ArrayList<>(videoItemHashSet);
return downloadedList;
}
String[] projection = { MediaStore.Video.Media._ID};
Cursor cursor = new CursorLoader(contexts, MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection,
null, // Return all rows
null, null).loadInBackground();
after you have the cursor you can iterate it and get all videos using the video id.
Try following code.
private void getVideoList() {
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Video.VideoColumns.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
ArrayList<String> pathArrList = new ArrayList<>();
if (cursor != null) {
while (cursor.moveToNext()) {
pathArrList.add(cursor.getString(0));
}
cursor.close();
}
Log.e("all path",pathArrList.toString());
}
Related
I add a custom contact raw to my contacts from my app (like in whatsapp). When I click the custom raw, it open my app. In my viewing activity I check the action and get the content URI from getIntent().getDataString(). It gives me this - content://com.android.contacts/data/10399.
Now I want to get the contact details from this URI. But when I check my contact list using the following code.
void checkContacts(Context context) {
Cursor cursor = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
null,
null, null, null);
if (cursor != null) {
Log.e(TAG, String.valueOf(cursor.getCount()));
try {
while (cursor.moveToNext()) {
Log.e(TAG, cursor.getString(cursor
.getColumnIndexOrThrow(ContactsContract.Data.CONTACT_ID)));
Log.e(TAG, "** " + cursor.getString(cursor
.getColumnIndexOrThrow(ContactsContract.Data.RAW_CONTACT_ID)));
}
} finally {
cursor.close();
}
}
}
But i couldn't find any CONTACT_ID or RAW_CONTACT_ID related to 10399.
So how do I get the contact detail from this URI?
I don't want to get all the contacts. I want to get the specific contact related to the URI.
try something like this
Uri uri = data.getData();
String[] projection = {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
};
Cursor cursor = getApplicationContext().getContentResolver().query(uri, projection,
null, null, null);
cursor.moveToFirst();
int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberColumnIndex);
int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String name = cursor.getString(nameColumnIndex);
cursor.close();
How can I get media titles from COntentResolver. i tried but it no works, i want to display media titles in my listView .
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
int ind = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
do {
String T = cursor.getString(ind);
abcl.add(T);
Log.i("SOngName", T);
} while (cursor.moveToNext());
}
but this gives me error of IndexOutOfBondsExceptions, my error logcat is
please any one guide , thanks
well first of all you should go for Cursor Documentation in android . it will tell you that after using the cursor is not null check; you have to move cursor to the start of first row on incoming result set.
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
if(cursor.MoveToFirst()){
int ind = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
do {
String T = cursor.getString(ind);
abcl.add(T);
Log.i("SOngName", T);
} while (cursor.moveToNext());
}
}
I have this code, to get all the photos saved on the phone.
String[] projection = {MediaStore.Images.Media._ID,MediaStore.Images.Media.DATA};
final Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.INTERNAL_CONTENT_URI,
projection,
null,
null,
null);
return cursor.getCount();
I am always getting the size of cursor as 0, when I try using this on emulator or on my phone. Any pointer will be helpful.
I really don't know how to test this on emulator.
I have the following permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Thanks
Here is code that worked.
public ArrayList<String> getCameraImages(Context context) {
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Media.DATA};
final Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null);
ArrayList<String> result = new ArrayList<String>(cursor.getCount());
Log.i("cursor.getCount()) :", cursor.getCount() + "");
if (cursor.moveToFirst()) {
final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
do {
final String data = cursor.getString(dataColumn);
Log.i ("data :",data );
result.add(data);
} while (cursor.moveToNext());
}
cursor.close();
return result;
}
i have a cursor that I am using in the following code. But I want to close the cursor after it is used and no longer needed. The problem is that the cursor is used in the return statement, but I can't close it after the return statement because that is unreachable code. It is used in the return statement so I can't close it above that line. How do I close the cursor? This is not like the old managedQuery, I assume that you have to close it.
public String getPath(Uri uri) {
String[] projection = { MediaStore.Audio.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
// cursor.close() <--- not possible because it is unreachable code after return
}
You can assign it to a String object then close cursor and return it.
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;
Would using a finally block work for you? I'm not experienced in your particular problem but could you put your cleanup in the finally block that would otherwise be bypassed by the return statement:
http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
Just an idea! Don't shoot me if not!
Try - Finally to the rescue...
public String getPath(Uri uri) {
Cursor cursor = null;
try {
String[] projection = { MediaStore.Audio.Media.DATA };
cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
finally {
if (cursor != null)
cursor.close(); // close the cursor
}
}
NOTE
Just noted another answer below..
If you're inside an activity, you can call startManaginCursor() and pass the cursor instance so Android will manage its lifetime. If you don't have access to Android Application Context (ex: code is in a utility class) use above method. (and sprinkle a bit of some error handling ;) )
if (this.daoDatabase != null) {
final Cursor genericCursor = this.daoDatabase.query(this.tableName, null, null, null, null, null, orderByCriteria);
if (genericCursor != null) {
if (genericCursor.moveToFirst()) {
do {
// do stuff...
} while (genericCursor.moveToNext());
}
genericCursor.close();
}
}
I know how to retrieve the genre of a particular song, (see getting the genres), but I want to retrieve all songs of a particular genre. Since "genre" does not seem to be one of the columns for a media item, I don't know how to do it in a single query, unlike artist or album. Is there an efficient method? Thanx!
Uri uri = Audio.Genres.Members.getContentUri("external", genreID);
String[] projection = new String[]{Audio.Media.TITLE, Audio.Media._ID};
Cursor cur = contentResolver.query(uri, projection, null, null, null);
You can do this by putting together a content URI and querying the MediaStore. Here's some code borrowed from the Android music player:
String [] cols = new String [] {MediaStore.Audio.Genres.NAME};
Cursor cursor = MusicUtils.query(this,
ContentUris.withAppendedId(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, Long.valueOf(mGenre)),
cols, null, null, null);
This is the code in MusicUtils:
public static Cursor query(Context context, Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder, int limit) {
try {
ContentResolver resolver = context.getContentResolver();
if (resolver == null) {
return null;
}
if (limit > 0) {
uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
}
return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
} catch (UnsupportedOperationException ex) {
ErrorReporter.getInstance().putCustomData("UnsupportedOperationException", "true");
return null;
}
}