解决安卓系统写入SD卡权限问题
生活随笔
收集整理的這篇文章主要介紹了
解决安卓系统写入SD卡权限问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.需要用戶手動賦予的權限( Dangerous Permissions)
| 所屬權限組 | 權限 |
|---|---|
| 日歷 | READ_CALENDAR |
| 日歷 | WRITE_CALENDAR |
| 相機 | CAMERA |
| 聯系人 | READ_CONTACTS |
| 聯系人 | WRITE_CONTACTS |
| 聯系人 | GET_ACCOUNTS |
| 位置 | ACCESS_FINE_LOCATION |
| 位置 | ACCESS_COARSE_LOCATION |
| 麥克風 | RECORD_AUDIO |
| 電話 | READ_PHONE_STATE |
| 電話 | CALL_PHONE |
| 電話 | READ_CALL_LOG |
| 電話 | WRITE_CALL_LOG |
| 電話 | ADD_VOICEMAIL |
| 電話 | USE_SIP |
| 電話 | PROCESS_OUTGOING_CALLS |
| 傳感器 | BODY_SENSORS |
| 短信 | SEND_SMS |
| 短信 | RECEIVE_SMS |
| 短信 | READ_SMS |
| 短信 | RECEIVE_WAP_PUSH |
| 短信 | RECEIVE_MMS |
| 存儲 | READ_EXTERNAL_STORAGE |
| 存儲 | WRITE_EXTERNAL_STORAGE |
2.具體增加權限操作
如圖所示,在mainfest文件中聲明了SD卡的讀和寫權限,仍舊報錯:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
安卓23.0版本以上,不僅僅要設置上面的權限,還要在對SD卡有讀寫操作的地方授權,下面是公共類:
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;public class PermisionUtils {// Storage Permissionsprivate static final int REQUEST_EXTERNAL_STORAGE = 1;private static String[] PERMISSIONS_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};/*** Checks if the app has permission to write to device storage* If the app does not has permission then the user will be prompted to* grant permissions** @param activity*/public static void verifyStoragePermissions(Activity activity) {// Check if we have write permissionint permission = ActivityCompat.checkSelfPermission(activity,Manifest.permission.WRITE_EXTERNAL_STORAGE);if (permission != PackageManager.PERMISSION_GRANTED) {// We don't have permission so prompt the userActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);}}
}
然后直接在需要授權的地方調用:注意接口函數一定要放對位置,血的教訓。
verifyStoragePermissions(this);
參考bolg:http://blog.csdn.net/qq_26787115/article/details/64127695
http://blog.csdn.net/wi2rfl78/article/details/78314286http://blog.csdn.net/htwhtw123/article/details/76032997http://blog.csdn.net/qq_26787115/article/details/64127695
總結
以上是生活随笔為你收集整理的解决安卓系统写入SD卡权限问题的全部內容,希望文章能夠幫你解決所遇到的問題。