You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Java代码获取任意Android手机本地联系人数据库路径?

Answers to Your Android Contacts Database Questions

Hey there! Since you're building a backup/restore app for Android contacts—awesome project, by the way—I'll walk you through both of your questions clearly:

1. Where is the Android Contacts Database stored?

The default storage path for the contacts database varies slightly by Android version and device configuration, but the most common locations are:

  • Standard user profile: /data/data/com.android.providers.contacts/databases/contacts2.db (this is the primary database for most modern Android versions; older versions might use contacts.db instead)
  • Multi-user or work profile: The path will include a user ID suffix, like /data/user/[user_id]/com.android.providers.contacts/databases/contacts2.db (replace [user_id] with the numeric ID of the target user, e.g., 0 for the main user)

Important note: This directory is private to the com.android.providers.contacts system app—regular third-party apps can't access it directly without root privileges or special system permissions.

2. How to get the Contacts DB path via Java code?

First, a critical reality check: For standard third-party apps (the kind you'd publish on Google Play), you cannot directly access or retrieve this path due to Android's sandboxed security model. The system restricts access to other apps' private data directories to protect user privacy.

Instead of trying to access the database file directly, you should use Android's official Contacts Provider API (via ContentResolver) to read and write contact data. This is the supported, secure way to interact with contacts on any Android device.

Example: Fetching contacts using ContentResolver

Here's a quick code snippet to retrieve basic contact details (name and phone number) using the official API:

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;

public void fetchContacts(ContentResolver contentResolver) {
    // Define the columns we want to retrieve
    String[] projection = {
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER
    };

    // Query the Contacts Provider
    Cursor cursor = contentResolver.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        projection,
        null,
        null,
        null
    );

    if (cursor != null) {
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            
            // Use the contact data (e.g., for backup)
            System.out.println("Name: " + name + ", Phone: " + phoneNumber);
        }
        cursor.close();
    }
}

If you're building a system app or have root access

If your app is part of the system ROM or you're working on a rooted device, you can retrieve the database path using the PackageManager to get the com.android.providers.contacts app's data directory:

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

public String getContactsDbPath(PackageManager packageManager) {
    try {
        ApplicationInfo appInfo = packageManager.getApplicationInfo("com.android.providers.contacts", 0);
        String dataDir = appInfo.dataDir;
        return dataDir + "/databases/contacts2.db";
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return null;
    }
}

Again, this will only work if your app has the necessary permissions (like android.permission.READ_CONTACTS plus system-level access or root privileges).


内容的提问来源于stack exchange,提问作者hadadrefael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:40:35