Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 读取外部存储权限目标sdk 29_Android_Android Permissions_Android External Storage - Fatal编程技术网

Android 读取外部存储权限目标sdk 29

Android 读取外部存储权限目标sdk 29,android,android-permissions,android-external-storage,Android,Android Permissions,Android External Storage,请求许可,我在我的简单音乐应用程序中请求读取外部存储的许可 它请求许可,但未在ListView中列出歌曲 明斯克版本27 targetSdkVersion 29 它显示了我终止应用程序并再次启动后的歌曲列表 这是我的密码: public class MainActivity extends AppCompatActivity { private int STORAGE_PERMISSION_CODE = 1; ListView lvMusic; MediaPlayer mp; String

请求许可,我在我的简单音乐应用程序中请求读取外部存储的许可 它请求许可,但未在ListView中列出歌曲 明斯克版本27 targetSdkVersion 29

它显示了我终止应用程序并再次启动后的歌曲列表

这是我的密码:

public class MainActivity extends AppCompatActivity {

private  int STORAGE_PERMISSION_CODE = 1;
ListView lvMusic;
MediaPlayer mp;
String name[],path[];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lvMusic = (ListView) findViewById(R.id.lvMusic);
    mp = new MediaPlayer();

    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            null,null,null,null);

    name = new String[cursor.getCount()];
    path = new String[cursor.getCount()];

    int i = 0;
    while(cursor.moveToNext()){
        name[i] =cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
        path[i] = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
        i++;
    }

    ArrayAdapter arrayAdapter =new ArrayAdapter(this, android.R.layout.simple_list_item_1,name);


    if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
        requestStoragePermission();

    }
   
    lvMusic.setAdapter(arrayAdapter);
}

private void requestStoragePermission(){
    if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)){
        new AlertDialog.Builder(this)
                .setTitle("Permission Needed")
                .setMessage("This Permission is needed to Read Files")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
                    }
                })
                .setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                 .create().show();
    }else{
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == STORAGE_PERMISSION_CODE ){
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this,"Permission Granted",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"Permission not Granted",Toast.LENGTH_SHORT).show();
        }
    }
}
}


提前谢谢

创建函数loadSongs,并在授予权限时调用它

void loadSongs(){
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            null,null,null,null);
    name = new String[cursor.getCount()];
    path = new String[cursor.getCount()];
    int i = 0;
    while(cursor.moveToNext()){
        name[i] =cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
        path[i] = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
        i++;
    }
    ArrayAdapter arrayAdapter =new ArrayAdapter(this, android.R.layout.simple_list_item_1,name);
    lvMusic.setAdapter(arrayAdapter);
)
现在,在您的on create中,检查权限是否已授予,如果已授予,请调用loadSongs

if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
    requestStoragePermission();
else
 loadSongs();
现在,在onRequestPermission结果中,如果授予了权限,则调用loadSongs:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == STORAGE_PERMISSION_CODE ){
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            loadSongs();
            Toast.makeText(this,"Permission Granted",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"Permission not Granted",Toast.LENGTH_SHORT).show();
        }
    }
}

谢谢,它解决了我的问题,但是你能解释一下为什么以前的代码不起作用,还有什么问题,这将是一个很大的帮助!提前谢谢@斯瓦普尼尔帕达亚没有任何问题。您在检查读取权限之前正在阅读歌曲。因此,如果未授予许可,则不会加载歌曲,然后您正在请求许可。因此,一旦获得许可,它将只显示祝酒词,因此要查看歌曲,您必须重新启动应用程序。谢谢!我明白我在做什么,谢谢你的时间!