0

I'm working on an app that save the local android Contacts DB in the server and you can download its back when you need (sort of backup) , and i have a question.

i need to find the path of the local contacts db in any android phone (using java code). how i can find it ? thanks.

ucMedia
  • 4,105
  • 4
  • 38
  • 46
  • You do not have filesystem-level access to the data inside of other apps, except on rooted devices. – CommonsWare Mar 24 '18 at 21:57
  • so what i can do ? – hadadrefael Mar 24 '18 at 22:01
  • You are welcome to retrieve contact data through the `ContactsContract` provider. This will require appropriate permissions. Restoring the data is unlikely to be necessary, as something else is the original source of most of the contact data (e.g., Google, Facebook). Few if any contacts will have the Android device as the "system of record" and need to be backed up in this fashion. – CommonsWare Mar 24 '18 at 22:13
  • Why do you think its a single file? Or in a single place on all phones? Those are implementation details that may not be universal. – Gabe Sechan Mar 24 '18 at 22:32

1 Answers1

0

Check this out. Also, this tutorial shows this in action.

You will need these permissions to access contacts:

<uses-permission android:name="android.permission.READ_CONTACTS">
<uses-permission android:name="android.permission.WRITE_CONTACTS">

This answer from similar question shows how to read all contacts at once. I'm copying it here for your convenience:

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

}
phones.close();
MatusMak
  • 560
  • 4
  • 20