我正在尝试查找存储中的所有mp3文件,但我的应用程序崩溃了,我使用logcat查看原因并且带有访问外部存储的权限问题,但我已经将权限放在清单上,我不知道为什么继续崩溃。 第一个错误:
03-06 14:55:24.553:E / dex2oat(2796):无法创建燕麦文件: /data/dalvik-cache/x86/data@app@felipe.cursoandroid.com.musicbox-2@split_lib_dependencies_apk.apk@classes.dex: 许可被拒绝
以及更多错误:
03-06 14:55:25.274:E / DatabaseUtils(1873):为包裹写例外 03-06 14:55:25.274:E / DatabaseUtils(1873): java.lang.SecurityException:Permission Denial:reading com.android.providers.media.MediaProvider uri content://来自pid = 2783的媒体/外部/音频/媒体,uid = 10057需要 android.permission.READ_EXTERNAL_STORAGE或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)
我的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);
}
}
我的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());
}
}
}
我的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>
答案 0 :(得分:0)
答案 1 :(得分:0)
如果您使用的是Android 23 +。那么我可以告诉您的问题是您需要明确地管理权限。读取/写入存储被视为危险权限。在这里,您可以了解如何管理:https://developer.android.com/training/permissions/requesting.html
您可以在Android API-22或下方运行您的应用。它应该在那里工作。