How to display the media title in a listview? - android

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());
}
}

Related

How to get the contact from the CONTENT URI

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();

where to close the cursor?

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();
}
}

Media Query Cursor keeps returning null on emulator and device....but why?

I am using the following code to get a list of songs on my device. Eventually I'd like to do more with them but to just get started I want to find audio/music in an Android device.
I have used this code to query the media store and I keep getting a null cursor... I've checked out these stack overflow answers but their either not relevant or I don't understand them enough to implement them...
Would appreciate any help! Thanks in advance
public class AudioFinalActivity extends Activity {
private TextView tv;
private String res;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// link text view obj
tv = (TextView) findViewById(R.id.tv);
res = "";
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Artists.ARTIST };
// managed query doesn't need startManagingCursor called on the
Cursor c = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
// ContentResolver contentResolver = getContentResolver();
// String[] columns = { MediaColumns.TITLE, AudioColumns.DURATION,
// MediaColumns.DATA
// // add more columns if you want to fetch more data
// };
//
// Cursor c = contentResolver.query(
// MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns, null,
// null, null);
if (c != null) {
Log.d("AFA", "Cursor returned NULL");
} else if (c.getCount() < 1) {
Log.d("AFA", "Cursor query is empty.. :( ...");
} else {
// do stuff with our content...
while (c.moveToNext()) {
//String title = c.getString(c
.getColumnIndex(MediaColumns.TITLE));
//Long duration = c.getLong(c
.getColumnIndex(AudioColumns.DURATION));
//String data = c.getString(c
.getColumnIndex(MediaColumns.DATA));
//res += title + "\n";
res += c.getString(c.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)) + "\n";
tv.setText(res);
}
}
}
}
I the stock music player can play my phones audio just fine..
EDIT:
I just removed my checking for the cursor being empty and for the cursor being null and I seem to get a result.. strange why the cursor is null yet the while(c.MoveToNext())... returns values... hmmm
your checking is if (c != null) {}, but I suppose it is if (c == null) {} (if you are checking the cursor being null)

How to get all videos on Android device

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());
}

using the contacts manager in android

i have an app which shows the received messages as a Toast through a BroadcastReceiver. I am presently using the SmsMessage.getOriginatingAddress() method which gives me the number of the sender, how can modify it to get the corresponding name of the sender if stored in the contacts?
You will need to query the contacts for the rest of the data.
First query for the contacts id using the phone number.
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, address),
new String[] { Contacts.Phones.PERSON_ID }, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
Long id = Long.valueOf(cursor.getLong(0));
if (Log.DEBUG) Log.v("Found person: " + id);
return (String.valueOf(id));
}
} finally {
cursor.close();
}
}
Then query for the Contacts name with the id from the first query.
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(Contacts.People.CONTENT_URI, id),
new String[] { PeopleColumns.DISPLAY_NAME }, null, null, null);
if (cursor != null) {
try {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
String name = cursor.getString(0);
if (Log.DEBUG) Log.v("Contact Display Name: " + name);
return name;
}
} finally {
cursor.close();
}
}
You may be able to combine these two queries somehow.

Categories

Resources