2016-09-20 27 views
1

私はアンドロイドで音楽プレーヤーを作成しようとしています。このコードは、mp3ファイルのsdカードを読んで、トーストでそれらを表示することです。AndroidのMp3/Music Player

私の携帯電話でこのアプリケーションを実行すると、電話機には空の画面が表示されます。つまり、曲は表示されません。

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; 
     } 
    } 
} 
} 

答えて

1

これで作業の答え:

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 
    } 
} 

}

+0

ありがとう! WRITE_EXTERNAL_STORAGEからREAD_EXTERNAL_STORAGEへアクセス権要求を変更する必要があることを除いて、すべて動作します。あなたの助けに感謝。 –

関連する問題