Reputation: 102
I'm trying to find all mp3 files in storage but my app is crashing and I used the logcat to see why and comes with problem with permission to access external storage but I already put the permission on manifest and I dont no why keep crashing. First error:
03-06 14:55:24.553: E/dex2oat(2796): Failed to create oat file: /data/dalvik-cache/x86/data@[email protected]@[email protected]: Permission denied
and more errors:
03-06 14:55:25.274: E/DatabaseUtils(1873): Writing exception to parcel 03-06 14:55:25.274: E/DatabaseUtils(1873): java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2783, uid=10057 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() 03-06 14:55:25.274: E/DatabaseUtils(1873): at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605) 03-06 14:55:25.274: E/DatabaseUtils(1873): at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480) 03-06 14:55:25.274: E/DatabaseUtils(1873): at android.content.ContentProvider$Transport.query(ContentProvider.java:211) 03-06 14:55:25.274: E/DatabaseUtils(1873): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) 03-06 14:55:25.274: E/DatabaseUtils(1873): at android.os.Binder.execTransact(Binder.java:453)
My MainActivity.java
:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SongsManager songsManager = new SongsManager();
songsManager.getSongs(this);
ArrayList<Song> songList = songsManager.getSongsList();
ArrayAdapter<Song> adapterSongList = new ArrayAdapter<Song>(this, android.R.layout.simple_list_item_1, songList);
ListView listView = (ListView) findViewById(R.id.musicList);
listView.setAdapter(adapterSongList);
}
}
My SongsManager.java
:
public class SongsManager {
private ArrayList<Song> songsList;
public ArrayList<Song> getSongsList(){
return songsList;
}
public void getSongs(Context ctx){
ContentResolver musicResolver = ctx.getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int pathConlumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisPath = musicCursor.getString(pathConlumn);
songsList.add(new Song(thisId, thisTitle, thisArtist, thisPath));
}
while (musicCursor.moveToNext());
}
}
}
my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="felipe.cursoandroid.com.musicbox">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 0
Views: 5519
Reputation: 17846
If you are using android 23+.Then I can tell that your issue is that you need to manage the permissions explicitly.Reading/Writing to Storage are considered to be dangerous permissions. Here you can learn how to manage that: https://developer.android.com/training/permissions/requesting.html
You can to run your app on Android API-22 or bellow. It should be working there.
Upvotes: 0
Reputation: 36
which android version you use? pay attention that for 6+ you should ask permissions on runtime
Upvotes: 0