I am trying to add the names of songs from an sd card on the phone to a list view but i am getting a null pointer exception...
private void updateList() {
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Artists.ARTIST };
Cursor tempCursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj,
null,
null,
null);
int col_index = -1;
int numSongs = tempCursor.getCount();
while (tempCursor.moveToNext()) {
col_index = tempCursor.getColumnIndexOrThrow(
MediaStore.Audio.Artists.ARTIST);
songname.add(tempCursor.getString(col_index));
}
ArrayAdapter<String> songss = new ArrayAdapter<String>(
this, R.id.songs, songname);
setListAdapter(songss);
}
I am just simply get songs data from sdcard as follows...It may helpful for you
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
if(cursor.moveToFirst())
{
for(int j=0;j<cursor.getCount();j++)
{
int ALBUM_ID = cursor.getInt((cursor
.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ID)));
String album_url = null;
Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, ALBUM_ID);
album_url = uri.toString();
ContentResolver res = this.getContentResolver();
// Album
String album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM));
String year = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.AudioColumns.YEAR));
// String year = cursor.getString(cursor
// .getColumnIndex(MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS));
// artist
String artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.ArtistColumns.ARTIST));
// displayname
String DisplayName = cursor.getString(cursor
.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
// title
String Title = cursor.getString(cursor
.getColumnIndex(MediaStore.MediaColumns.TITLE));
}
Related
I am using
Uri uriSms = Uri.parse("content://mms-sms/conversations?simple=true");
Cursor cursor = getContentResolver().query(uriSms, null,null,null,null);
cursor.moveToLast();
while (cursor.moveToPrevious())
{
String recipient_ids= cursor.getString(cursor.getColumnIndex("recipient_ids"));
String body = cursor.getString(cursor.getColumnIndex("snippet"));
}
to get a list of sms conversation.
The "recipient_ids" returns some value Like 302, 301 259 etc. What I want is a function where i will pass the "recipient_ids" as perameter and it will return the display name of the contact (If available else the Number)
These methods help you to achieve what you want:
getContactByRecipientId - to get contact number by recipientId.
getContactbyPhoneNumber - to get display name by phone number.
public String getContactByRecipientId(long recipientId) {
String contact = "";
Uri uri = ContentUris.withAppendedId(Uri.parse("content://mms-sms/canonical-address"), recipientId);
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
try {
if (cursor.moveToFirst()) {
contact = getContactbyPhoneNumber(cursor.getString(0));
}
} finally {
cursor.close();
}
return contact;
}
public String getContactbyPhoneNumber(String phoneNumber) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NORMALIZED_NUMBER };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
String name = null;
String nPhoneNumber = phoneNumber;
try {
if (cursor.moveToFirst()) {
nPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.NORMALIZED_NUMBER));
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
} finally {
cursor.close();
}
if(name != null){ // if there is a display name, then return that
return name;
}else{
return nPhoneNumber; // if there is not a display name, then return just phone number
}
}
Please check this code
private void fetchInbox() {
Uri inboxURI = Uri.parse("content://sms/inbox");
String[] reqCols = new String[]{"_id", "address", "body"};
ContentResolver cr = getContentResolver();
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber}, 0);
}
I want to have a ListView on my app, which populates all mp3 files on my external storage.
Here is the code in the onCreate method.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) findViewById(R.id.listView1);
List<DoubleString> mList = new ArrayList<DoubleString>();
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE;
ContentResolver resolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = resolver.query(uri, null, selection, null, sortOrder);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
int title = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int artist = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int data = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
do {
long mId = cursor.getLong(id);
String mTitle = cursor.getString(title);
String mArtist = cursor.getString(artist);
String mData = cursor.getString(data);
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(mData);
String mimeType = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
if (mimeType == "audio/mpeg") {
mList.add(new DoubleString(mId, mTitle, mArtist, mData));
}
}
while (cursor.moveToNext());
}
final DoubleStringAdapter adapter = new DoubleStringAdapter(this, mList);
mListView.setAdapter(adapter);
but the ListView returns null, i.e. Doesn't populate anything on ListView.
So my question is:
does my code works for filtering?
or is there any method to do this using MediaStore & MediaMetadataRetriever?
You can do it by below code which use MediaStore.Please add the external storage permission.
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
The code is -
public ArrayList<HashMap<String, String>> getSDCardAudioFiles() {
// if you want file path and additional details
ArrayList<HashMap<String, String>> audioFilesDetailList = new ArrayList<HashMap<String, String>>();
// if you want only file path
ArrayList<String> audioFilePath =new ArrayList<String>();
// if you want only file name
ArrayList<String> audioFileName =new ArrayList<String>();
Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA }, null, null, null);
int count = mCursor.getCount();
HashMap<String, String> audioFileMap;
while (mCursor.moveToNext()) {
audioFileMap = new HashMap<String, String>();
audioFileMap.put("FileName",mCursor.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
audioFileMap.put("FilePath", mCursor.getString(mCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
if(audioFileMap.get("FileName").endsWith(".mp3")){
audioFilesDetailList.add(audioFileMap);
// if you want only file path
audioFilePath.add(audioFileMap.get("FilePath"));
// if you want only file name
audioFileName.add(audioFileMap.get("FileName"));
}
}
mCursor.close();
//return the arraylist whichever u needed
return audioFilesDetailList;
}
I'm trying to get name of Contact List from phone's contacts list, I have error on first line of try: ContentResolver cr = ContactFragment.getActivity().getContentResolver();
Any help?
ContactFragment.java
private String refreshData() {
String namedata = "";
try {
/**************************************************/
ContentResolver cr = ContactFragment.getActivity().getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + namedata + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(namedata));
phones.close();
}
}
}
Try Like This :
Set this tag in manifest file:
<uses-permission android:name="android.permission.READ_CONTACTS" />
Try this code :
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
int contactIdIdx = cursor.getColumnIndex(Phone._ID);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
cursor.moveToFirst();
do {
String idContact = cursor.getString(contactIdIdx);
String name = cursor.getString(nameIdx);
String phoneNumber = cursor.getString(phoneNumberIdx);
//...
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
I have problem in a database that contains the data that already exists, but the data does not appear in the textbox. please help
this is data from Form.java
String modul;
String itemreg;
modul = dataSource.getModule(assignmentid,orderid,productmoduleid,productdetailid);
//itemreg = dataSource.getItemreg(productmoduleid, productdetailid);
if(Globals.modul==null){
Globals.modul=modul;
}
String namamodul;
namamodul=dataSource.getModul1(productmoduleid);
edModul.setText(namamodul);
and this is dbdatasource.java
public String getModule(String assignmentid, String orderid, String productmoduleid, String productdetailid){
Cursor cursor = database.query(DBHelper.ASSIGNMENT_ORDER_DETAIL,allAssignemntOrderDetail, DBHelper.ASSIGNMENT_ID +"= 'assgn1' and "+DBHelper.ORDER_ID +"= 'ord1'", null, null, null, null);
cursor.moveToFirst();
AssignmentOrderDetail = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_MODULE_ID));
cursor.close();
return AssignmentOrderDetail;
}
public String getModul1(String productmoduleid){
String productmodul ;
Cursor cursor = database.query(DBHelper.PRODUCT_MODULE,allProductModule, DBHelper.PRODUCT_MODULE_ID +"='"+Globals.modul+"'", null, null, null, null);
cursor.moveToFirst();
productmodul = cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_MODULE_DESC));
cursor.close();
return productmodul;
}
try if this could help:
try{
Cursor cursor =
database.query(DBHelper.ASSIGNMENT_ORDER_DETAIL,allAssignemntOrderDetail,
DBHelper.ASSIGNMENT_ID +"= 'assgn1' and "+DBHelper.ORDER_ID +"= 'ord1'",
null, null, null, null);
if(cursor != null) {
cursor.moveToFirst();
AssignmentOrderDetail =
cursor.getString(cursor.getColumnIndex(DBHelper.PRODUCT_MODULE_ID));
}
return AssignmentOrderDetail;
}finally {
cursor.close();
}
Try with below code:
Cursor cursor = null;
String TABLE_NAME = "yourTableName";
String[] allColumns = {"coloumn1","coloumn2","coloumn3"};
String whereClause = "coloumn1"+"=?";
String[] whereArgs = new String[] {method_parameter};
Like below:
private Cursor getData(String param_id)
{
try{
sqliteDb = appDb.getWritableDatabase();
if(sqliteDb.isOpen())
{
cursor = sqliteDb.query(TABLE_NAME, whereClause, whereArgs);
}
}catch(Exception ex)
{
b = false;
ex.printStackTrace();
}
return cursor;
}
I need to get the name, email and phone number from each contact of my contacts list.
The problem is I can get name and phone number, but not the email. I'm getting the phone number instead of email.
Here is my code:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String email = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
ContactItem objContact = new ContactItem();
objContact.setName(name);
objContact.setPhoneNo(phoneNumber);
objContact.setEmail(email);
list.add(objContact);
}
phones.close();
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
//May want to get basic info here like name, phone
//Example:
//Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
//while (phones.moveToNext()) {
// String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
// Log.i("phone", phoneNumber);
//}
//phones.close();
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.i("emails", emailAddress);
}
emails.close();
}
cursor.close();
Source and Reference:
How to read contacts on Android 2.0
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
String phoneNumber = null;
String name = null;
String emailAddress = null;
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
Cursor cursor = null;
cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
// May want to get basic info here like name, phone
// Example:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
while (phones.moveToNext()) {
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("phone", phoneNumber);
}
phones.close();
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
null, null);
while (emails.moveToNext()) {
emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.i("emails", emailAddress);
}
emails.close();
}
cursor.close();
Log.i(DEBUG_TAG, "Got a contact result: " + name + emailAddress + "Phone Number is :- " + phoneNumber);
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}
}
}
}