Android 的 READ_EXTERNAL_STORAGE 权限
Posted
技术标签:
【中文标题】Android 的 READ_EXTERNAL_STORAGE 权限【英文标题】:READ_EXTERNAL_STORAGE permission for Android 【发布时间】:2015-12-02 14:23:15 【问题描述】:我正在尝试访问用户设备上的媒体文件(音乐)来播放它们;一个简单的“hello world”——音乐播放器应用。
我遵循了一些教程,它们基本上给出了相同的代码。但这行不通;它一直在崩溃并告诉我:
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()
....
现在,这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="slimsimapps.troff" >
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
<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:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的 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());
我试过了:
将其放在清单文件中的不同位置:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE”/>
在读取外部存储权限时添加 android:maxSdkVersion:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="21" />
把它放在清单/应用程序/活动标签中:
android:exported=“true”
在java方法中将grantUriPremission放在uri和cursro之间:
grantUriPermission(null, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
使用它不会崩溃,但是光标变为空:
uri = MediaStore.Audio.Media.getContentUri("EXTERNAL_CONTENT_URI”);
要使用 INTERNAL 内容 uri,这可以按预期工作,但它只提供“操作系统声音”,例如快门声音、低电量声音、按钮点击等:
uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
请帮忙,我知道这应该不是一个难题,但我觉得自己像个初学者!
我已阅读并尝试过(或认为它们不适用于我的问题):
Android READ_EXTERNAL_STORAGE permission not working Require permission only for older android versions: maxSdkVersion does not work? Get filename and path from URI from mediastore Android KitKat securityException when trying to read from MediaStore Android: java.lang.SecurityException: Permission Denial: start Intent堆栈跟踪:
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
【问题讨论】:
奇怪的是,当你没有在清单中给出这一行时会显示错误您的问题有两种解决方案。快速的方法是将 targetApi 降低到 22(build.gradle 文件)。 二是使用新奇的请求许可模型:
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_EXTERNAL_STORAGE))
// Explain to the user why we need to read the contacts
requestPermissions(new String[]Manifest.permission.READ_EXTERNAL_STORAGE,
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant that should be quite unique
return;
在此处找到代码段:https://developer.android.com/training/permissions/requesting.html
解决方案 2:如果它不起作用,试试这个:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.READ_EXTERNAL_STORAGE,
REQUEST_PERMISSION);
return;
然后在回调中
@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
// Permission granted.
else
// User refused to grant permission.
来自 cmets。谢谢
【讨论】:
是的,当然!谷歌最新安卓系统引入的新权限模型!谢谢,我会尽快测试它,如果它有效,我会标记它是正确的! (我开始认为这被遗忘了!) 请求的权限应该是READ_EXTERNAL_STORAGE
。比如this answer。
交换 != PackageManager.READ_EXTERNAL_STORAGE 为 != PackageManager.PERMISSION_GRANTED
@Renascienza,是的,你是对的。有人以错误的方式编辑了此答案。
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE 是什么?【参考方案2】:
您必须在运行时请求许可:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.READ_EXTERNAL_STORAGE,
REQUEST_PERMISSION);
dialog.dismiss();
return;
在下面的回调中,您可以毫无问题地访问存储。
@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
// Permission granted.
else
// User refused to grant permission.
【讨论】:
【参考方案3】:Step1:在 android manifest.xml 上添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
第二步:onCreate() 方法
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE, MY_PERMISSIONS_REQUEST_READ_MEDIA);
else
readDataExternal();
Step3:重写onRequestPermissionsResult方法获取回调
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
switch (requestCode)
case MY_PERMISSIONS_REQUEST_READ_MEDIA:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED))
readDataExternal();
break;
default:
break;
注意:readDataExternal() 是从外部存储中获取数据的方法。
谢谢。
【讨论】:
我认为您在这里放错了所需的权限:是 READ_EXTERNAL_STORAGE。【参考方案4】:我也有类似的错误日志,这就是我所做的-
在 onCreate 方法中,我们请求一个对话框来检查权限
ActivityCompat.requestPermissions(MainActivity.this,
new String[]Manifest.permission.READ_EXTERNAL_STORAGE,1);
检查结果的方法
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
switch (requestCode)
case 1:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
// permission granted and now can proceed
mymethod(); //a sample method called
else
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
return;
// add other cases for more permissions
官方文档发给Requesting Runtime Permissions
【讨论】:
我用过它,它运行良好,它适用于所有安卓设备吗?【参考方案5】:您的问题解决了吗?你的目标 SDK 是什么?尝试将android;maxSDKVersion="21"
添加到<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
【讨论】:
使用 Cordova 为我工作,仅将 AndroidManifest.xml 中的最大 SDK 版本设置为 22(而不是 23)【参考方案6】:2021 年我面临这个问题 所以我将使用 我在应用程序中添加一个标签的清单文件:
<application
android:requestLegayExternalStorage="true"
/>
注意:这仅在 Android 10 之前有效,请参阅更多信息 here
【讨论】:
requestLegacyExternalStorage *typo【参考方案7】:http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html
尝试换行contentResolver.query
//change it to the columns you need
String[] columns = new String[]MediaStore.Audio.AudioColumns.DATA;
Cursor cursor = contentResolver.query(uri, columns, null, null, null);
公共静态最终字符串 MEDIA_CONTENT_CONTROL
由于媒体消费隐私,第三方应用程序不得使用
http://developer.android.com/reference/android/Manifest.permission.html#MEDIA_CONTENT_CONTROL
先尝试删除<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
,然后重试?
【讨论】:
谢谢,我尝试了您的建议,但它在同一个地方崩溃了...我使用的是 Mac,这可能是个问题吗? 已更新,尝试删除<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
并尝试?它可能会产生奇怪的行为,因为您的应用请求权限不允许
谢谢,我试图删除该权限,但它在同一个地方崩溃并出现同样的错误......【参考方案8】:
请检查以下代码,使用该代码您可以从 sdcard 中找到所有音乐文件:
public class MainActivity extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animations);
getAllSongsFromSDCARD();
public void getAllSongsFromSDCARD()
String[] STAR = "*" ;
Cursor cursor;
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = managedQuery(allsongsuri, STAR, selection, null, null);
if (cursor != null)
if (cursor.moveToFirst())
do
String song_name = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
int song_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media._ID));
String fullpath = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
String album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int album_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
String artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int artist_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
System.out.println("sonng name"+fullpath);
while (cursor.moveToNext());
cursor.close();
我还在 AndroidManifest.xml 文件中添加了以下行,如下所示:
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
【讨论】:
谢谢,我之前尝试过类似的方法,但我尝试了您的代码,但它在“cursor = managedQuery(allsongsuri, STAR, selection, null, null);”上崩溃,与以前相同的错误。顺便一提; managedQuery 已弃用,但如果我将其更改为具有相同输入参数的“contentResolver.query”,它仍然会崩溃... 嗨,我可以使用上面的代码获取所有 sdcadr 歌曲。使用 Android 4.2.2 版设备,也可以使用 5.0.2 版。你在其中尝试。你遇到了什么错误请用Android版本描述 我已经用堆栈跟踪更新了这个问题,希望对您有所帮助。安卓版是什么意思? (我使用的是 apk 23) 好的,让我提供完整的源代码并再次检查。 如果您想自己测试,请在此处下载 zip:drive.google.com/open?id=0B2AV8VRk9HUeVzFXSWVRZU9TRUk 运行它时,按左侧初始化按钮,生成 crach。【参考方案9】:除了所有答案。 您还可以指定要与此注释一起应用的 minsdk
@TargetApi(_apiLevel_)
我使用它是为了接受这个请求,即使我的 minsdk 是 18。它的作用是该方法仅在设备以“_apilevel_”和更高版本为目标时运行。 这是我的方法:
@TargetApi(23)
void solicitarPermisos()
if (ContextCompat.checkSelfPermission(this,permiso)
!= PackageManager.PERMISSION_GRANTED)
// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_EXTERNAL_STORAGE))
// Explain to the user why we need to read the contacts
requestPermissions(new String[]Manifest.permission.READ_EXTERNAL_STORAGE,
1);
// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant that should be quite unique
return;
【讨论】:
以上是关于Android 的 READ_EXTERNAL_STORAGE 权限的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )