我正在尝试访问用户设备上的媒体文件(音乐)来播放它们;一个简单的“你好世界” – 音乐播放器应用程序.
我已经按照一些教程,他们基本上给出了相同的代码.但它不起作用;它一直在崩溃并告诉我:
error.....
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=27696, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
....
现在,这是我的清单文件:
xmlns:android="http://schemas.android.com/apk/res/android"
package="slimsimapps.troff" >
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/app_name" >
这是我的Java方法:
public void initialize() {
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
// query failed, handle error.
} else if (!cursor.moveToFirst()) {
// no media on the device
} else {
do {
addSongToXML(cursor);
} while (cursor.moveToNext());
}
}
我试过了:
将它放在清单文件中的不同位置:
android:maxSdkVersion="21" />
将它放在manifest / application / activity-tag中:
android:exported=“true”
在javamethod中将grantUriPremission放在uri和cursro之间:
grantUriPermission(null, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
要使用它,它不会崩溃,但游标变为null:
uri = MediaStore.Audio.Media.getContentUri("EXTERNAL_CONTENT_URI”);
要使用内部内容uri,这可以按预期工作,但它只提供“操作系统声音”,如快门声,低电池声,按钮点击等:
uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
请求帮助,这不应该是我知道的难题,但我觉得这样的初学者!
我已阅读并尝试过(或认为它们不适用于我的问题):
堆栈跟踪:
09-08 06:59:36.619 2009-2009/slimsimapps.troff D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
09-08 06:59:36.619 2009-2009/slimsimapps.troff E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: slimsimapps.troff, PID: 2009
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
????????????at android.view.View.performClick(View.java:5198)
????????????at android.view.View$PerformClick.run(View.java:21147)
????????????at android.os.Handler.handleCallback(Handler.java:739)
????????????at android.os.Handler.dispatchMessage(Handler.java:95)
????????????at android.os.Looper.loop(Looper.java:148)
????????????at android.app.ActivityThread.main(ActivityThread.java:5417)
????????????at java.lang.reflect.Method.invoke(Native Method)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2009, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:491)
at android.content.ContentResolver.query(ContentResolver.java:434)
at slimsimapps.troff.MainActivity.initialize(MainActivity.java:106)
at slimsimapps.troff.MainActivity.InitializeExternal(MainActivity.java:80)
????????????at java.lang.reflect.Method.invoke(Native Method)
????????????at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
????????????at android.view.View.performClick(View.java:5198)
????????????at android.view.View$PerformClick.run(View.java:21147)
????????????at android.os.Handler.handleCallback(Handler.java:739)
????????????at android.os.Handler.dispatchMessage(Handler.java:95)
????????????at android.os.Looper.loop(Looper.java:148)
????????????at android.app.ActivityThread.main(ActivityThread.java:5417)
????????????at java.lang.reflect.Method.invoke(Native Method)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
--------- beginning of system