Reputation:
I am trying to access mp3 files in externalStorage and this is my code:
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();
}
}
}
This is my 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>
This is my 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)
As you can see i have added all the permissions required but still I am getting permission Denied error Can someone help me out with this?? ThankYou
Upvotes: 2
Views: 4578
Reputation: 392
If the device is running Android 5.1 or lower, or your app's target SDK is 22 or lower: If you list a dangerous permission in your manifest, the user has to grant the permission when they install the app; if they do not grant the permission, the system does not install the app at all.
If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request. use below code,
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.
}
}
and handle you permission status by,
@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
}
}
Source : http://developer.android.com/training/permissions/requesting.html
Upvotes: 0
Reputation: 5325
If you are running it on marshmallow, you need to request the permission at the runtime.
Here's the official doc : http://developer.android.com/training/permissions/requesting.html
You have to check the permission using ContextCompat.checkSelfPermission
function. Then, if you don't have the permission,
request it via ActivityCompat.requestPermissions
method and implement
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
method to receive the user results.
Upvotes: 1