2016-06-28 181 views
2

我的Android應用有權限問題: 這裏是日誌。Android SecurityException:權限拒絕

06-25 17:25:35.862 12039-13799/? E/DatabaseUtils﹕ Writing exception to parcel 
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=1580, uid=10108 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() 
     at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605) 
     at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480) 
     at android.content.ContentProvider$Transport.query(ContentProvider.java:211) 
     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) 
     at android.os.Binder.execTransact(Binder.java:453) 



06-25 17:25:35.870 1580-2997/? E/iu.UploadsManager﹕ Insufficient permissions to access media store 
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=1580, uid=10108 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() 
     at android.os.Parcel.readException(Parcel.java:1599) 
     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) 
     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) 
     at android.content.ContentProviderProxy.query(ContentProviderNative.java:421) 
     at android.content.ContentResolver.query(ContentResolver.java:498) 
     at android.content.ContentResolver.query(ContentResolver.java:441) 
     at ifh.i(PG:5904) 
     at ifh.h(PG:619) 
     at ifh.<init>(PG:187) 
     at iel.c(PG:4045) 
     at gen_binder.root.RootModule$Generated.a(PG:1131) 
     at nmw.e(PG:415) 
     at nmw.b(PG:242) 
     at nmw.a(PG:207) 
     at nmw.a(PG:493) 
     at ier.a(PG:2195) 
     at ier.onPerformSync(PG:125) 
     at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:272) 


06-25 17:24:37.031 1580-1611/? E/GooglePlusContactsSync﹕ Failed to clear out contacts 
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2ForLG from ProcessRecord{9557af9 1580:com.google.android.apps.plus/u0a108} (pid=1580, uid=10108) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS 
     at android.os.Parcel.readException(Parcel.java:1599) 
     at android.os.Parcel.readException(Parcel.java:1552) 
     at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3652) 
     at android.app.ActivityThread.acquireProvider(ActivityThread.java:4866) 
     at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2020) 
     at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1488) 
     at android.content.ContentResolver.query(ContentResolver.java:482) 
     at android.content.ContentResolver.query(ContentResolver.java:441) 
     at dpm.a(PG:397) 
     at dpm.b(PG:1345) 
     at dpn.run(PG:282) 
     at java.lang.Thread.run(Thread.java:818) 

我知道這是一個權限問題,我可以與人的許可這樣解決:

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, 
       Manifest.permission.READ_CONTACTS) 
     != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
      Manifest.permission.READ_CONTACTS)) { 

     // Show an expanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(thisActivity, 
       new String[]{Manifest.permission.READ_CONTACTS}, 
       MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

     // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

,然後以此來權限設置爲我的供應商:

@Override 
public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

問題是我不知道在我的項目的哪一部分我必須把這個權限請求: 我使用Auth0自定義UI登錄,Mysqlite作爲內部數據庫se和GoogleCards的ListView。

你能幫我理解如何授予這個權限拒絕嗎?

非常感謝你

+0

我希望你給予權限清單文件readcontact –

回答

1

那麼我會推薦使用Ask,它是一個權限助手庫。

使用這個,你不必編寫這麼多的代碼,只需兩行就可以完成你的任務。

下面是使用細節

首先編譯依賴於你的build.gradle

dependencies { 
    compile 'com.vistrav:ask:2.4' 
} 

,然後在活動開始在OnCreate或任何按下按鈕,你可以問這樣

權限
Ask.on(this) 
    .forPermissions(Manifest.permission.READ_CONTACTS) 
    .go(); 

這就是它。

你可以找到更多的信息here。我幾乎在所有項目中都使用它,而且它簡單易用。

希望它可以幫助

+0

哇這個問題庫看起來很簡單! – HendraWD

0

當然,你必須把它放在任何活動。此功能是否需要運行您的應用程序?詢問應用程序開始。如果你的應用程序可以在沒有它的情況下工作 - 在不可避免的情況下閱讀聯繫人(在一個視圖中)。

0
Basically you have to check for these permission just before the code where you want to access the contacts. 

    First of all check for the android version,if it is >=23 then 

    if (ContextCompat.checkSelfPermission(thisActivity, 
        Manifest.permission.READ_CONTACTS) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
       Manifest.permission.READ_CONTACTS)) { 

      // Show an expanation to the user *asynchronously* -- don't block 
      // this thread waiting for the user's response! After the user 
      // sees the explanation, try again to request the permission. 

     } else { 

      // No explanation needed, we can request the permission. 

      ActivityCompat.requestPermissions(thisActivity, 
        new String[]{Manifest.permission.READ_CONTACTS}, 
        MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

      // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
      // app-defined int constant. The callback method gets the 
      // result of the request. 
     } 
    } 
    else 
    { 
    // do here contact related work say you wwant to fetch the name and number from contact,you can do that task here. 
    } 

You have to override this method: 


@Override 
public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
0

的Android版本低於6.0簡單地定義你的清單在堆棧跟蹤(READ_CONTACTS,WRITE_CONTACTS等)中提到的權限會工作。

對於android 6.0及更高版本,您將不得不要求用戶授予權限。理想情況下,應該在絕對必要時完成,例如當用戶導航到需要該權限的應用程序部分時。這是解釋here