我是Android的编程新手并尝试构建测试应用程序play music
但是当我尝试在Android模拟器中运行应用程序时,我一直收到此错误。任何帮助将不胜感激。
01/02 12:25:31: Launching app
$ adb push C:\GPA CALC\playmusic\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.wordpress.mbedo.www.playmusic
$ adb shell pm install -r "/data/local/tmp/com.wordpress.mbedo.www.playmusic"
Success
$ adb shell am start -n "com.wordpress.mbedo.www.playmusic/com.wordpress.mbedo.www.playmusic.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 2293 on device emulator-5554
W/System: ClassLoader referenced unknown path: /data/app/com.wordpress.mbedo.www.playmusic-2/lib/x86
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wordpress.mbedo.www.playmusic, PID: 2293
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wordpress.mbedo.www.playmusic/com.wordpress.mbedo.www.playmusic.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2293, uid=10053 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2293, uid=10053 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1683)
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:530)
at android.content.ContentResolver.query(ContentResolver.java:472)
at com.wordpress.mbedo.www.playmusic.MainActivity.onCreate(MainActivity.java:57)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
申请已终止。
答案 0 :(得分:0)
你需要在运行时为6.0以上的Android版本调用权限
答案 1 :(得分:0)
当您的logcat指示时,您的应用程序正在尝试访问/读取您的内部存储,Android系统拒绝该存储。 Android版本> = M。
需要权限 java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2293, uid=10053 requires android.permission.READ_EXTERNAL_STORAGE
在Android应用中,您必须要求用户获得访问其存储空间的权限。
// if android version >= M
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission
(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
}
在征得许可后,你必须处理它:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
{
switch (requestCode) {
case 0:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
// permission granted
}
else {
//not granted
}
break;
}
}
}