-3

I try to play a music with MediaPlayer. The problem is I don't succed to acces file even if I ask for permissions in manifest

My Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nkm.metaextract" >
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

and the java code:

public class MainActivity extends AppCompatActivity {

//public class MetaExtractActivity extends Activity {
    ImageView album_art;
    TextView album, artist, genre;
    MediaPlayer mp;

    MediaMetadataRetriever metaRetriever;
    byte[] art;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getInit();

        File dir =Environment.getExternalStorageDirectory();
        File file=new File(dir + File.separator +"Music/Wholesome.mp3");
        if (file.exists()) {
            genre.setText(file.toString());
            Log.i("DEBUG", "Trouvé "+file.toString());

        }else {genre.setText("pas trouvé");
            Log.i("DEBUG", "pas trouvé");
        }

        mp = new MediaPlayer();
        try {
            mp.setDataSource("/storage/emulated/0/Music/Wholesome.mp3");
            Log.i("DEBUG", "ok");
            artist.setText("ok");
        } catch (IOException e) {
            e.printStackTrace();
            Log.i("DEBUG", "Erreur  "+ e);
            artist.setText("pas trouvé");
        }

Nota: the file exist and i can read it with store apps

I tried to change de path whit "/storage/emulated/0/Music/Wholesome.mp3" and few other way without change

Where i test if it's a file I obtain: "I/DEBUG: Trouvé /storage/emulated/0/Music/Wholesome.mp3" but just next "W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Music/Wholesome.mp3 (Permission denied)"

and where i try to acces I obtain: "I/DEBUG: Erreur java.io.FileNotFoundException: /storage/emulated/0/Music/Wholesome.mp3 (Permission denied)"

When app start, Android doesn't ask me for acces...and app can't acces file.

is somebody can help ?

Mike M.
  • 38,532
  • 8
  • 99
  • 95

2 Answers2

0

Since Android 6.0 you have to ask for the permission in the runtime:

https://developer.android.com/training/permissions/requesting

What you can do for now, is to go to settings of the app, go to permission section, enable those permissions and restart the app. If it doesn't work, I would add runtime permission checks from the link I posted above

Mariusz Brona
  • 1,549
  • 10
  • 12
0

Thank you very much Mariusz.

I add

            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

and it wors well...