Read_EXTERNAL_STORAGE 权限目标 sdk 29
Read_EXTERNAL_STORAGE Permission Target sdk 29
请求许可,我正在请求我的简单音乐应用程序读取外部存储的许可
它请求许可但没有列出 ListView
中的歌曲
minSdkVersion 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);
)
现在在你的创建中,检查权限是否被授予,如果被授予调用 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();
}
}
}
请求许可,我正在请求我的简单音乐应用程序读取外部存储的许可
它请求许可但没有列出 ListView
中的歌曲
minSdkVersion 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);
)
现在在你的创建中,检查权限是否被授予,如果被授予调用 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();
}
}
}