I have this function from developer.android but it can't find all songs, only few. Please give me some clue.
public void getAllSongs() {
ContentResolver contentResolver = getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
// query failed, handle error.
} else if (!cursor.moveToFirst()) {
// no media on the device
} else {
int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
do {
long thisId = cursor.getLong(idColumn);
String thisTitle = cursor.getString(titleColumn);
songs.add(thisId + "||"+thisTitle);// ...process entry...
} while (cursor.moveToNext());
}
}
I use the following to get all tracks:
private final String track_id = MediaStore.Audio.Media._ID;
private final String track_no = MediaStore.Audio.Media.TRACK;
private final String track_name = MediaStore.Audio.Media.TITLE;
private final String artist = MediaStore.Audio.Media.ARTIST;
private final String artist_id = MediaStore.Audio.Media.ARTIST_ID;
private final String duration = MediaStore.Audio.Media.DURATION;
private final String album = MediaStore.Audio.Media.ALBUM;
private final String composer = MediaStore.Audio.Media.COMPOSER;
private final String year = MediaStore.Audio.Media.YEAR;
private final String path = MediaStore.Audio.Media.DATA;
private final String date_added = MediaStore.Audio.Media.DATE_ADDED;
private final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
public Cursor getAllTracks(Context context) {
// gets all tracks
ContentResolver cr = context.getContentResolver();
final String[] columns = {track_id ,track_no, artist, track_name,
album, duration, path, year, composer};
return cr.query(uri, columns, null, null, null);
}
Related
It is possible to recognize whether the file obtained using "Media file access" is stored on the SD card or in the Internal Memory ? For example, is it possible to find out the information from a media URI? In my opinion, it is only possible to find out the name of a subdirectory, but then it is no longer possible to find out the name of parent subdirectory.
Thanks, Michal
class Video {
private final Uri uri;
private final String name;
private final int duration;
private final int size;
public Video(Uri uri, String name, int duration, int size) {
this.uri = uri;
this.name = name;
this.duration = duration;
this.size = size;
}
}
List<Video> videoList = new ArrayList<Video>();
String[] projection = new String[] {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.SIZE
};
String selection = MediaStore.Video.Media.DURATION +
" >= ?";
String[] selectionArgs = new String[] {
String.valueOf(TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES));
};
String sortOrder = MediaStore.Video.Media.DISPLAY_NAME + " ASC";
try (Cursor cursor = getApplicationContext().getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
)) {
// Cache column indices.
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
int nameColumn =
cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
int durationColumn =
cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
while (cursor.moveToNext()) {
// Get values of columns for a given video.
long id = cursor.getLong(idColumn);
String name = cursor.getString(nameColumn);
int duration = cursor.getInt(durationColumn);
int size = cursor.getInt(sizeColumn);
Uri contentUri = ContentUris.withAppendedId(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
// Stores column values and the contentUri in a local object
// that represents the media file.
videoList.add(new Video(contentUri, name, duration, size));
}
}
it is possible to somehow find out that the video is on the SD card or in the memory.
I want to retrieve whole audio_genres_map table from mediastore in one query. I have all the audio info and genre info but the missing piece is the mapping. I am able to do it in multiple queries. I want to achieve it one query
managed to solve this using following query
String[] genresProjection = {
Audio.Genres.Members.AUDIO_ID,
Audio.Genres.Members.GENRE_ID
};
context.getContentResolver().query(Uri.parse("content://media/external/audio/genres/all/members"), genresProjection, null, null, null);
Or to retrieve all genres using MediaStore.Audio
public Cursor getGenrecursor(Context context) {
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Audio.Genres.getContentUri("external");
String genre = MediaStore.Audio.Genres.NAME;
final String[] columns = { "_id", genre };
return cr.query(uri, columns, null, null, null);
}
and to expand your question, using the genre_id to bring back further details
public Cursor getgenre_TrackCursor(Context context, long genreID) {
final String track_id = MediaStore.Audio.Genres.Members._ID;
final String track_no = MediaStore.Audio.Genres.Members.TRACK;
final String track_name = MediaStore.Audio.Genres.Members.TITLE;
final String artist = MediaStore.Audio.Genres.Members.ARTIST;
final String duration = MediaStore.Audio.Genres.Members.DURATION;
final String album = MediaStore.Audio.Genres.Members.ALBUM;
final String composer = MediaStore.Audio.Genres.Members.COMPOSER;
final String year = MediaStore.Audio.Genres.Members.YEAR;
final String path = MediaStore.Audio.Genres.Members.DATA;
final ContentResolver resolve = context.getContentResolver();
Uri uri = MediaStore.Audio.Genres.Members.getContentUri("external",
genreID);
final String[] columns = { track_id, track_no, artist, track_name,
album, duration, year };
return resolve.query(uri, columns, null, null, null);
}
When I send a sms in Android mobile phone, I can select multiple contact (Please see A.png ), and the sms will be as one record.
Can the sms be query using "content://sms/" ? will the field "address" return multiple phone numbers?
Thanks!
A.png
public static List<String> ListDeleteOld(Context myContext, SMSRange mSMSRange, int beforeDays ) {
List<String> mListSmsID=new ArrayList<String>();
Uri uri=PublicParFun.GetUriBySMSRange(mSMSRange);
Date beforeDate=getCurrentBefore(beforeDays);
String[] projection = new String[] {"_id","address"};
Cursor cur = myContext.getContentResolver().query(uri, projection, "date<=" + beforeDate.getTime(), null, "date desc");
while(cur.moveToNext()){
String s=cur.getString(cur.getColumnIndex("address"));
}
cur.close();
return mListSmsID;
}
public static Uri GetUriBySMSRange(SMSRange mSMSRange){
Uri uri=null;
final String SMS_URI_ALL = "content://sms/";
final String SMS_URI_INBOX = "content://sms/inbox";
final String SMS_URI_SEND = "content://sms/sent";
final String SMS_URI_OUTBOX = "content://sms/outbox";
final String SMS_URI_DRAFT = "content://sms/draft";
switch (mSMSRange){
case All:
uri = Uri.parse(SMS_URI_ALL);
break;
case Inbox:
uri = Uri.parse(SMS_URI_INBOX);
break;
case Sentbox:
uri = Uri.parse(SMS_URI_SEND);
break;
case Outbox:
uri = Uri.parse(SMS_URI_OUTBOX);
break;
case Draft:
uri = Uri.parse(SMS_URI_DRAFT);
break;
}
return uri;
}
set variables for example:
private static final Uri SMS_SENT_URI = Uri.parse("content://sms/sent");
private static final String SMS_ORDER = "date DESC";
private static final String ADDRESS_COLUMN_NAME = "address";
private static final String DATE_COLUMN_NAME = "date";
private static final String BODY_COLUMN_NAME = "body";
private static final String TYPE_COLUMN_NAME = "type";
private static final String ID_COLUMN_NAME = "_id";
private static final String SMS_PREFERENCES = "SMS_PREFERENCES";
You have to crete SmsObject.
Then just loop trough Cursor.
The whole code: (call checkNewOutgoingSms(context))
private void checkNewOutgoingSms(Context context) {
Cursor smsCursor = getSmsCursor(context);
List<Sms> smsList = getLastSmsList(smsCursor,context);
if (smsList != null && smsList.size() > 0) {
ProtectorWSODao mwtProtectorDao = new ProtectorWSODao();
for (Sms sms : smsList) {
//
//
//read sms content
//
//
}
Manager.getInstance(context).sendDataToServer(mwtProtectorDao);
}
smsCursor.close();
}
public static Cursor getSmsCursor(Context context) {
return context.getContentResolver().query(SMS_SENT_URI, null, null, null, SMS_ORDER);
}
private List<Sms> getLastSmsList(Cursor smsCursor, Context context) {
List<Sms> smsList = new ArrayList<Sms>();
final int lastSmsIntercepted = smsStorage.getLastSmsIntercepted();
boolean update = false;
if (smsCursor != null) {
if (smsCursor.moveToFirst()) {
do {
Sms smsParsed = parseSms(smsCursor, context);
smsList.add(smsParsed);
} while (smsCursor.moveToNext());
}
}
return smsList;
}
public static Sms parseSms(Cursor cursor, Context context) {
String number = cursor.getString(cursor.getColumnIndex(ADDRESS_COLUMN_NAME));
String date = cursor.getString(cursor.getColumnIndex(DATE_COLUMN_NAME));
String content = cursor.getString(cursor.getColumnIndex(BODY_COLUMN_NAME));
int smsId = cursor.getInt(cursor.getColumnIndex(ID_COLUMN_NAME));
return new Sms(Sms.SEND, smsId, number, date, content);
}
I try to get all tracks from MediaStore.
Cursor cursor = managedQuery(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.AudioColumns._ID));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST));
int duration = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION));
}
cursor.close();
But I can't read values from cursor. When I run this code
MediaStore.Audio.AudioColumns.TITLE,
MediaStore.Audio.AudioColumns.ALBUM,
MediaStore.Audio.AudioColumns.ARTIST and
MediaStore.Audio.AudioColumns.DURATION
constants is null.
Which constants I must use to get values in result above?
set up columns and uri to return:
final String track_id = MediaStore.Audio.Media._ID;
final String track_no = MediaStore.Audio.Media.TRACK;
final String track_name = MediaStore.Audio.Media.TITLE;
final String artist = MediaStore.Audio.Media.ARTIST;
final String duration = MediaStore.Audio.Media.DURATION;
final String album = MediaStore.Audio.Media.ALBUM;
final String composer = MediaStore.Audio.Media.COMPOSER;
final String year = MediaStore.Audio.Media.YEAR;
final String path = MediaStore.Audio.Media.DATA;
final String date_added = MediaStore.Audio.Media.DATE_ADDED;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
To get all tracks:
public Cursor getTrackTrackcursor(Context context, Cursor cursor) {
// gets all tracks
ContentResolver cr = context.getContentResolver();
final String[] columns = { track_id, track_no, artist, track_name,
album, duration, path, year, composer };
cursor = cr.query(uri, columns, null, null, null);
return cursor;
}
now loop through the cursor and extract the string values according to the columns returned.
You can only extract columns which have been returned by the cursor so an error would occur as I did not return NUMBER_OF_SONGS. You would have to ensure this is also returned by the cursor
For example:
String stralbum = (c.getString(cursor
.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));
String strartist = (cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Albums.ARTIST)));
String stryear = (cursor.getString(cusrsor
.getColumnIndex(MediaStore.Audio.Albums.FIRST_YEAR)));
String strnosongs = (cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS)));
You are 99% correct but your uri needs the column layout. Hope this helps
I am looking for a way to get the contacts in the favorites list inside service from phone number or from name it dose not matter. Can any one help me with this?
It's not important to use any code related to this code
I found in the developer.android.com something like this (IN_VISIBLE_GROUP).
How to use this variable in my case?
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
ContentResolver cr = getContentResolver();
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
String id =c.getString(c.getColumnIndexOrThrow(People._ID));
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + id, null, null);
}
Lets assume that you are searching a contact by name..
If you want to get Favourite value of all the possible contacts , drop the selection parameter in the given code.
//First get the contact ID from a display name as:-
String displayName = "Albert Einstein";
Uri contacts = ContactsContract.Contacts.CONTENT_URI;
cur = cr.query(contacts, null, ContactsContract.Contacts.DISPLAY_NAME +"="+displayName,null, null);
int contactIdIndex = cur.getColumnIndex(ContactsContract.PhoneLookup._ID);
int contactId = cur.getInt(contactIdIndex);
//Make a query to get the Starred value:-
Cursor starred = cr.query(ContactsContract.Contacts.CONTENT_URI,
new String[] { ContactsContract.Contacts.STARRED },
ContactsContract.Contacts._ID + " = " + contactId,
null, null);
if (starred != null && starred.moveToFirst())
{
int fav = starred.getInt(0);
}
if (starred != null)
starred.close();
}
You can drop the step of getting Contact ID and then querying for Starred value and directly query based on Display name
Something like this?
final private static class DataQuery {
public static final int COLUMN_MIMETYPE = 1;
public static final int COLUMN_PHONE = 2;
public static final int COLUMN_RAWCONTACT_ID = 3;
public static final int COLUMN_PHONE_NUMBER = COLUMN_DATA1;
public static final String[] PROJECTION = new String[] { Data._ID, Data.MIMETYPE, Data.DATA1, Data.RAW_CONTACT_ID };
public static final String SELECTION_PHONE = Data.DATA1 + "=?";
}
long findContact(Context context, String number) {
long rawContactId = -1;
final Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI, DataQuery.PROJECTION, DataQuery.SELECTION_PHONE, new String[] { number }, null);
try {
if (cursor.moveToFirst()) {
rawContactId = cursor.getLong(DataQuery.COLUMN_RAWCONTACT_ID);
}
} finally {
if (cursor != null)
cursor.close();
}
return rawContactId;
}
Ok let's try with this...
private static final Uri DATAGROUP_CONTENT_URI = ContactsContract.Data.CONTENT_URI.buildUpon().appendQueryParameter(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE).build();
public static void querytGroups(Context context) {
final ContentResolver resolver = context.getContentResolver();
long groupid=getGroupId(resolver, "Family");
final Cursor c = resolver.query(DATAGROUP_CONTENT_URI, DataQueryForContactsInGroup.PROJECTION, DataQueryForContactsInGroup.SELECTION, new String[] {ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupid)}, null);
try {
while (c.moveToNext()) {
final long rawContactId = c.getLong(DataQueryForContactsInGroup.RAW_CONTACT_ID);
//do something
}
}finally {
c.close();
}
}
private static long getGroupId(final ContentResolver resolver, String groupName) {
long groupid = -1;
Cursor cur = null;
try {
cur = resolver.query(Groups.CONTENT_URI, DataQueryForGroup.PROJECTION, DataQueryForGroup.SELECTION, new String[]{"%"+groupName+"%"}, null);
while (cur.moveToNext()) {
return groupid= cur.getLong(DataQueryForGroup.GROUP_ID);
}
}finally {
if (cur!=null) cur.close();
}
return groupid;
}
private interface DataQueryForGroup {
public final static String[] PROJECTION = new String[] {Groups._ID};
public static final String SELECTION = Groups.TITLE+" LIKE ?";
public final static int GROUP_ID = 0;
}
private interface DataQueryForContactsInGroup {
public final static String[] PROJECTION = new String[] { Data.RAW_CONTACT_ID };
public static final String SELECTION = "("+Data.MIMETYPE + "=?) and ("+ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID+ "=?)";
public final static int RAW_CONTACT_ID = 0;
}
Please consider that if your google account is not English you need to look for the proper group's name