Mediastore albums and media columns - java

Too retrieve the album id i use:
String SONG_ALBUMID = MediaStore.Audio.Albums.ALBUM_ID;
But what is the difference between MediaStore.Audio.Albums.ALBUM_ID and MediaStore.Audio.Media.ALBUM_ID
Thanks,

MediaStore.Audio.Albums.ALBUM_ID is the album id from the albums table and
MediaStore.Audio.Media.ALBUM_ID will be the "foreign key" album_id in the files table.In general relational database design is such that when accessing the files table, all we need is the abum_id to access the albums table and retrieve all relevant albums info such as album_title.The table files in the android database holds lots of details
eg
_id INTEGER
_data TEXT
_size INTEGER
format INTEGER
parent INTEGER
date_added INTEGER
date_modified INTEGER
mime_type TEXT
title TEXT
description TEXT
_display_name TEXT
picasa_id TEXT
orientation INTEGER
latitude DOUBLE
longitude DOUBLE
datetaken INTEGER
mini_thumb_magic INTEGER
bucket_id TEXT
bucket_display_name TEXT
isprivate INTEGER
title_key TEXT
artist_id INTEGER
album_id INTEGER
composer TEXT
track INTEGER
year INTEGER
is_ringtone INTEGER
is_music INTEGER
is_alarm INTEGER
is_notification INTEGER
is_podcast INTEGER
album_artist TEXT
duration INTEGER
bookmark INTEGER
artist TEXT
album TEXT
resolution TEXT
tags TEXT
category TEXT
language TEXT
mini_thumb_data TEXT
name TEXT
media_type INTEGER
old_id INTEGER
storage_id INTEGER
is_drm INTEGER
width INTEGER
height INTEGER

Here are 2 approaches:
(include the requested columns in your resolver.query)
public String getalbum_id(Cursor pcursor) {
int album_col = pcursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);
String album_id = pcursor.getString(album_col);
if (album_id.length() > 0) {
return album_id;
} else {
return "";
}
}
public String getAlbumIdfromPath(Context context, String datapath) {
ContentResolver cr = context.getContentResolver();
final String _id = MediaStore.Audio.Media._ID;
final String path = MediaStore.Audio.Media.DATA;
final String album_id = MediaStore.Audio.Media.ALBUM_ID;
final String[] columns = {_id, album_id};
final String[] trackpath = {"%" + datapath + "%"};
String where = path + " LIKE ?";
String stralbum_id = null;
Cursor crs = cr.query(uri, columns, where, trackpath, null);
if (crs != null && crs.moveToFirst()) {
stralbum_id = crs.getString(crs.getColumnIndexOrThrow(album_id));
crs.close();
}
return stralbum_id;
}
now to display, I have posted previously but I use the Gilde library. It does all the hard work.
// loading album cover using Glide library
String artist_id = (c.getString(c
.getColumnIndex(MediaStore.Audio.Artists._ID)));
Cursor albs = albums.getArtistsAlbumcursor(mContext, artist_id);
String stralbumId=null;
if(albs!=null && albs.moveToFirst()){
stralbumId= albs.getString(albs.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
}
Uri ImageUrl = plist.getAlbumUri(mContext, stralbumId);
if (ImageUrl != null) {
Glide.with(mContext)
.asBitmap()
.load(ImageUrl)
.into(image);
}
}
where getAlbumuri is:
public Uri getAlbumUri(Context mContext,String album_id){
if(mContext!=null) {
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri imageUri = Uri.withAppendedPath(sArtworkUri, String.valueOf(album_id));
return imageUri;
}
return null;
}
Include the following in your module build.gradle
implementation 'com.github.bumptech.glide:glide:4.5.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
private final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

Related

Slow loading times in MusicPlayerApp

Hey so I just made an android app which basically fetches all the mp3/wav files from your phone and then shows them it to you. Which you can then play them. But I am experiencing slow load times. My phone has like 10 mp3 and its a 6GB RAM phone. But it takes like 1 minute to load all those files. I am using the built in MediaPlayer Class. Any tips?
Use this code.
for reference link
public void getMp3Songs() {
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
songsList.clear();
Cursor cursor = managedQuery(allsongsuri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String song_name = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
int song_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media._ID));
String fullpath = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
songsList.add(fullpath);
String album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int album_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
String artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int artist_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
} while (cursor.moveToNext());
}
cursor.close();
}
Toast.makeText(context, ""+songsList.size(), Toast.LENGTH_SHORT).show();
}

MediaStore External Content URI is fetching data from SDCARD

I am using MediaStore and Cursor to fetch audio files from a specified folder. What I want is that, it should fetch the list of songs from the phone's internal storage but it is fetching from SDCARD. Although I have tried using INTERNAL_CONTENT_URI but it is not getting anything at all. Kindly have a look, what's wrong in it. Any help will be appreciated.
String AudioFilePath = "%" +"/nexcox/voicerecorder/audio/" +"%";
Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection= MediaStore.Audio.Media.DATA +" LIKE ? ";
Cursor cursor=context.getContentResolver().query(uri,null,selection,
new String[]{AudioFilePath},
null);
if (cursor!=null){
if (cursor.moveToFirst()){
do {
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String sourceLocation = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
AudioInfo audioInfo=new AudioInfo(name,duration,sourceLocation);
audioInfos.add(audioInfo);
}while (cursor.moveToNext());
}
cursor.close();
audioAdapter=new AudioAdapter(context,audioInfos);
recyclerView.setAdapter(audioAdapter);
}
for example
String fullpath = trackcursor.getString(trackcursor.getColumnIndex(MediaStore.Audio.Media.DATA))
if(fullpath.contains(Environment.getExternalStorageDirectory(){
// you have the internal sdcard
}
In your code:
if (cursor!=null){
if (cursor.moveToFirst()){
do {
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String sourceLocation = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
if(sourceLocation.contains(Environment.getExternalStorageDirectory(){
// you have the internal sdcard
AudioInfo audioInfo=new AudioInfo(name,duration,sourceLocation);
audioInfos.add(audioInfo);
}
}while (cursor.moveToNext());
}
cursor.close();
audioAdapter=new AudioAdapter(context,audioInfos);
recyclerView.setAdapter(audioAdapter);
}
I use the same approach as your example. Works fine.
public Cursor getMediastoreTrackDetails(Context context, String trackName) {
if(context!=null) {
ContentResolver cr = context.getContentResolver();
final String _id = MediaStore.Audio.Media._ID;
final String path = MediaStore.Audio.Media.DATA;
final String album = MediaStore.Audio.Media.ALBUM;
final String album_id = MediaStore.Audio.Media.ALBUM_ID;
final String[] columns = {_id, path,album, album_id};
final String[] trackname = {"%" + trackName + "%"};
String where = path + " LIKE ?";
return cr.query(uri, columns, where, trackname, null);
}else{
return null;
}
}

Inserting a value into a URL in android studio?

I sent data from Activity A to Activity B through means of an intent.
I then extracted the results of the intent via the following.
String IdString = questionData.getString("id");
TextView mstudentId = (TextView) findViewById(R.id.ID);
mstudentId.setText(IdString);
I would now like to take the value of IdString which is equal to the value of the string "id" and place it inside a Url. For example,
final static String URL_ANSWER = "http//:myFake/(IdString)/Url"
How do I go about doing this in Android Studio?
Like this,
final static String URL_ANSWER = "http//:myFake/" + IdString + "/Url";
OR
UPDATE
String IdString = questionData.getString("id");
TextView mstudentId = (TextView) findViewById(R.id.ID);
mstudentId.setText(IdString);
final static String URL_ANSWER = "http//:myFake/" + mstudentId.getText() + "/Url";

ConcurrentModificationException when I iterate on ArrayList

I want to iterate on an ArrayList called localWifiList that contains the wifi networks detected by a wifi scan.
For every element of the ArrayList I want to run a query to get all tuples in the database with that specific mac address, create a new object and add this object into a new arrayList called wifiFromDatabase.
I wrote this code:`
ArrayList<wifiList> wifiFromDatabase = new ArrayList<wifiList>();
ArrayList<wifiList> localWifiList = ScanService.wifiArraList;
//field to read the values of wifi query results
String mac;
String ssid;
String cid;
String signalLevel;
String capabilities;
String rssi;
String lat, lng;
String date;
String frequency;
int flagInt;
Cursor cursor;
Iterator<wifiList> iterator = localWifiList.iterator();
while(iterator.hasNext()){
wifiList element = (wifiList) iterator.next();
cursor = MainActivity.getDBOperationHelper().getWifiTupleByMac
(MainActivity.getDBOperationHelper().getReadableDatabase(), element.getMacAddress());
if(cursor.getCount()>0){
if (cursor .moveToFirst()) {
while (cursor.isAfterLast() == false) {
mac = cursor.getString(cursor.getColumnIndex(DBOperationHelper.MAC));//
ssid = cursor.getString(cursor.getColumnIndex(DBOperationHelper.SSID));//
capabilities = cursor.getString(cursor.getColumnIndex(DBOperationHelper.CAPABILITIES));//
frequency = cursor.getString(cursor.getColumnIndex(DBOperationHelper.FREQUENCY));//
cid = cursor.getString(cursor.getColumnIndex(DBOperationHelper.CELL_ID_UMTS));//
signalLevel = cursor.getString(cursor.getColumnIndex(DBOperationHelper.SIGNAL_LEVEL_WIFI));//
rssi = cursor.getString(cursor.getColumnIndex(DBOperationHelper.RSSI));
lat = cursor.getString(cursor.getColumnIndex(DBOperationHelper.GPS_LATITUDE_WIFI));//
lng = cursor.getString(cursor.getColumnIndex(DBOperationHelper.GPS_LONGITUDE_WIFI));//
date = cursor.getString(cursor.getColumnIndex(DBOperationHelper.DATE_WIFI));//
flagInt = cursor.getInt(cursor.getColumnIndex(DBOperationHelper.FLAG));
wifiList objectFromDb = WifiPhoneConfiguredNetworkHandler.CreateProperlyWifiListObject(ssid, capabilities, frequency, signalLevel, ConnectionPointAnalyzer.INVALID_ID_WIFI, signalLevel,
mac, rssi, date, cid, lat, lng, flagInt, false);
wifiFromDatabase.add(objectFromDb);
cursor.moveToNext();
}
}
}else{ //the database has not tuples with this mac
Log.d(ConnectionPointAnalyzer.LOG_TAG, "OracoloBrain.java/AllInterfacesActived: no tuples found in the db with mac = "+element.getMacAddress()+
" ssid = "+element.getSsid());
}
} `
where the method CreateProperlyWifiListObject create an wifiList object given the fields passed as arguments.
I read many thread about this issues, but nothing to do. I try also with synchronized on the arrayList.
The exception is thrown by iterator.next() command.
Try to create a copy:
ArrayList<wifiList> localWifiList = new ArrayList<wifiList>(ScanService.wifiArraList);

Android: I have a list of contact names, how to get their corresponding emails and pohone numbers?

I have an activity that has a listview which displays all contacts with multi-selection enabled. I have also a filter to filter the display result.
After struggling with this problem for 3 days, I finally could produce a list of all display names selected by the user.
My question now is how to get the corresponding email and phone number for each display name in my list?
Update:
I found a solution..
public String getNumber(String name,Context context){
String number="";
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = context.getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
String Name = people.getString(indexName);
String Number = people.getString(indexNumber);
if(Name.equalsIgnoreCase(name)){return Number.replace("-", "");}
// Do work...
} while (people.moveToNext());
if(!number.equalsIgnoreCase("")){return number.replace("-", "");}
else return number;
}
public String getEmail(String name,Context context){
String email="";
Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA};
Cursor people = context.getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
people.moveToFirst();
do {
String Name = people.getString(indexName);
String mail = people.getString(indexNumber);
if(Name.equalsIgnoreCase(name)){return mail;}
// Do work...
} while (people.moveToNext());
if(!email.equalsIgnoreCase("")){return email;}
else return email;
}
Got it from there:
Android get contact number using name

Categories

Resources