我正在尝试在android中创建音乐播放器。此代码用于读取mp3文件的sd卡,并在烤面包中显示它们。
当我在手机上运行此应用程序时,手机将显示空白屏幕,即未显示任何歌曲。
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
String items;
final int READ_EXTERNAL_STORAGE_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE_REQUEST);
return;
}
ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
for(int i = 0; i < mySongs.size(); i++) {
toast(mySongs.get(i).getName().toString());
}
}
public ArrayList<File> findSongs(File root) {
ArrayList<File> al = new ArrayList<File>();
File[] files = root.listFiles();
for(File singleFile : files) {
if(singleFile.isDirectory() && !singleFile.isHidden()) {
al.addAll(findSongs(singleFile));
}
else {
if(singleFile.getName().endsWith(".mp3")) {
al.add(singleFile);
}
}
}
return al;
}
public void toast(String text) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch(requestCode) {
case READ_EXTERNAL_STORAGE_REQUEST: if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
return;
}
}
}
}
最佳答案
这是有效的答案:
public class MainActivity extends AppCompatActivity {
TextView countTextview;
ListView listView;
private List<String> arrayList;
ArrayAdapter arrayAdapter;
final private static int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countTextview = (TextView)findViewById(R.id.count);
arrayList = new ArrayList<String>();
listView = (ListView)findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
permissionManager();
}
public void add(){
ContentResolver cr = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cur = cr.query(uri, null, selection, null, sortOrder);
int count = 0;
if(cur != null)
{
count = cur.getCount();
if(count > 0)
{
while(cur.moveToNext())
{
String data = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
arrayList.add(data);
}
listView.setAdapter(arrayAdapter);
}
}
cur.close();
}
public void permissionManager(){
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}else {
add();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
add();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Permission is really required to get Songs", Toast.LENGTH_SHORT).show();
permissionManager();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}