Android で電話帳から選択した人の電話番号一覧を取得するプログラムを作成しましたが、
以下のエラーが発生していて原因がわからないです。
ご教授いただきたいです。
よろしくお願いいたします。
■エラーのデバック内容
**E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.sample, PID: 20758 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1111, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/258r132-2606083C030D6026260816030D602668/115 flg=0x1 }} to activity {com.example.sample/com.example.sample.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=20758, uid=10344 requires android.permission.READ_CONTACTS, or grantUriPermission() at android.app.ActivityThread.deliverResults(ActivityThread.java:4382) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4424) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1814) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:280) at android.app.ActivityThread.main(ActivityThread.java:6706) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=20758, uid=10344 requires android.permission.READ_CONTACTS, or grantUriPermission() at android.os.Parcel.createException(Parcel.java:1969) at android.os.Parcel.readException(Parcel.java:1935) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.query(ContentProviderNative.java:418) at android.content.ContentResolver.query(ContentResolver.java:804) at android.content.ContentResolver.query(ContentResolver.java:753) at android.content.ContentResolver.query(ContentResolver.java:711) at com.example.sample.MainActivity._getPhoneNumbers(MainActivity.java:122) at com.example.sample.MainActivity.onActivityResult(MainActivity.java:70) at android.app.Activity.dispatchActivityResult(Activity.java:7454) at android.app.ActivityThread.deliverResults(ActivityThread.java:4375) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4424) at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1814) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:280) at android.app.ActivityThread.main(ActivityThread.java:6706) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) I/Process: Sending signal. PID: 20758 SIG: 9
■作成したプログラム ```ここに言語を入力 ```ここに言語を入力 package com.example.sample; import android.Manifest; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentResolver; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import androidx.core.app.ActivityCompat; import com.mkyong.android.R; /** * @author peko * */ public class MainActivity extends Activity implements DialogInterface.OnClickListener { private static final String TAG = "MainActivity"; private static final int PICK_CONTACT = 1111; private static final int REQUEST_CODE = 1000; private AlertDialog.Builder _alertDialogBuilder; private TextView _phoneNumberTextView; private String[] _phoneNumbers; /* * (non-Javadoc) * * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); this._alertDialogBuilder = new AlertDialog.Builder(this); this._phoneNumberTextView = (TextView) this.findViewById(R.id.textView); ((Button) this.findViewById(R.id.button)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent pickIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(pickIntent, MainActivity.PICK_CONTACT); } }); } /* * (non-Javadoc) * * @see android.app.Activity#onActivityResult(int, int, * android.content.Intent) */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == MainActivity.PICK_CONTACT) { if (resultCode == Activity.RESULT_OK) { if (null != data) { Uri contactData = data.getData(); String displayName = this._getDisplayName(contactData); this._phoneNumbers = this._getPhoneNumbers(displayName); if (this._phoneNumbers.length > 1) { this._createNumberSelectAlertDialog(displayName, this._phoneNumbers); } else if (this._phoneNumbers.length == 1) { this._phoneNumberTextView.setText(this._phoneNumbers[0]); } } } } } /* * (non-Javadoc) * * @see * android.content.DialogInterface.OnClickListener#onClick(android.content * .DialogInterface, int) */ @Override public void onClick(DialogInterface dialog, int which) { String selectedPhoneNumber = this._phoneNumbers[which]; Uri uri = Uri.parse("tel:" + selectedPhoneNumber); Intent i = new Intent(Intent.ACTION_CALL,uri); startActivity(i); } /** * コンタクトリストのURIから表示名を取得する * * @param contactData * @return */ private String _getDisplayName(Uri contactData) { String displayName = null; if (null != contactData) { ContentResolver contentResolver = this.getContentResolver(); Cursor cursor = contentResolver.query(contactData, null, null, null, null); cursor.moveToFirst(); int columnIndex = cursor .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); displayName = cursor.getString(columnIndex); cursor.close(); } return displayName; } // // コンタクトリストから表示名で検索して、その名前に紐付いている電話番号を取得する // // @param displayName // @return // private String[] _getPhoneNumbers(String displayName) { ContentResolver contentResolver = this.getContentResolver(); Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + "=?", new String[] { displayName }, null); cursor.moveToFirst(); String[] names = new String[cursor.getCount()]; do { int index = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); String number = cursor.getString(index); names[cursor.getPosition()] = number; } while (cursor.moveToNext()); cursor.close(); return names; } /** * 電話番号選択のダイアログを出す * * @param title * @param items */ private void _createNumberSelectAlertDialog(String title, String[] items) { this._alertDialogBuilder.setTitle(title); this._alertDialogBuilder.setItems(items, this); AlertDialog dialog = this._alertDialogBuilder.create(); dialog.show(); } }
回答1件
あなたの回答
tips
プレビュー