我正在尝试访问 externalStorage 中的mp3文件,这是我的代码:
public class MainActivity extends AppCompatActivity {
private LinearLayout viewL;
private String[] STAR = { "*" };
String fullpath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewL = (LinearLayout) findViewById(R.id.linear);
try{
String provider = "com.android.providers.media.MediaProvider";
Uri uri = Uri.parse("content://media/external/audio/media");
grantUriPermission(provider, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
grantUriPermission(provider, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
grantUriPermission(provider, uri, Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
Cursor cursor;
Uri allsongsuri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = android.provider.MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = getContentResolver().query(allsongsuri, STAR, selection, null, null);
for(int i =0;i<1;i++){
cursor.moveToFirst();
fullpath = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
}
MediaPlayer mp = new MediaPlayer();
Uri myUri = Uri.parse(fullpath);
mp.setDataSource(this,myUri);
mp.prepare();
mp.start();
}
catch (Exception e){
Snackbar.make(viewL,e.toString(),Snackbar.LENGTH_LONG).show();
}
}
}
这是我的manifestFile:
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.grassa"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
这是我的logcat:
Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=20241, uid=10113 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)
正如您所看到的,我已经添加了所有必需的权限,但我仍然获得权限拒绝错误有人可以帮我解决这个问题吗? THANKYOU
答案 0 :(得分:1)
如果您在marshmallow上运行它,则需要在运行时请求权限。
这是官方文件:http://developer.android.com/training/permissions/requesting.html
您必须使用ContextCompat.checkSelfPermission
功能检查权限。然后,如果您没有获得许可,
通过ActivityCompat.requestPermissions
方法申请并实施
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
方法来接收用户结果。
答案 1 :(得分:0)
如果设备运行的是Android 5.1或更低版本,或者您的应用的目标SDK为22或更低:如果您在清单中列出了危险权限,则用户必须在安装应用时授予权限;如果他们不授予权限,系统根本不会安装应用程序。
如果设备运行的是Android 6.0或更高版本,并且您的应用的目标SDK为23或更高:应用必须列出清单中的权限,并且必须在应用运行时请求其所需的每个危险权限。用户可以授予或拒绝每个权限,即使用户拒绝权限请求,应用程序也可以继续以有限的功能运行。 使用下面的代码,
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
}
}
来源:http://developer.android.com/training/permissions/requesting.html