找不到媒体播放器文件异常

时间:2018-08-09 06:11:09

标签: android android-mediaplayer

我正在编写一个基本的android mp3播放器应用程序,该应用程序使用MediaPlayer应用程序播放本地存储在Android手机上的mp3文件。

但是,每当我尝试致电MediaPlayer.create()时,我都会得到java.io.FileNotFoundException

我检查了mp3的正确路径,并尝试在模拟的Android和我自己的手机上运行mp3,但我一直遇到相同的错误。

这是我遇到问题的onCreate函数:

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

    Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath() + 
       "/Download/sam_stone.mp3");

    med = MediaPlayer.create(getApplicationContext(), uri);
}

这是我遇到的错误:

  

java.io.FileNotFoundException:   /storage/emulated/0/Download/sam_stone.mp3(权限被拒绝)                  在java.io.FileInputStream.open(本机方法)                  在java.io.FileInputStream。(FileInputStream.java:146)                  在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1088)                  在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1067)                  在android.media.MediaPlayer.setDataSource(MediaPlayer.java:1009)                  在android.media.MediaPlayer.setDataSource(MediaPlayer.java:967)                  在android.media.MediaPlayer.create(MediaPlayer.java:874)                  在android.media.MediaPlayer.create(MediaPlayer.java:851)                  在android.media.MediaPlayer.create(MediaPlayer.java:830)                  在hn.mediaplayer.MainActivity.onCreate(MainActivity.java:48)                  在android.app.Activity.performCreate(Activity.java:6662)                  在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                  在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)                  在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                  在android.app.ActivityThread.-wrap12(ActivityThread.java)                  在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                  在android.os.Handler.dispatchMessage(Handler.java:102)                  在android.os.Looper.loop(Looper.java:154)                  在android.app.ActivityThread.main(ActivityThread.java:6077)                  在java.lang.reflect.Method.invoke(本机方法)                  在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866)                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

3 个答案:

答案 0 :(得分:1)

AndroidManifest.xml

中添加权限
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

,并在android 5.0以上版本中添加运行时权限

答案 1 :(得分:1)

第一件事是您需要向 AndiodManifest.xml 文件

添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

然后在您的活动中

public  boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
    if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        return true;
    } else {

        Log.v(TAG,"Permission is revoked");
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        return false;
    }
}
else { 
    Log.v(TAG,"Permission is granted");
    return true;
}}

然后返回权限结果,您可以在其中继续执行任务:

@Override
 public void onRequestPermissionsResult(int requestCode, String[] permissions, 
 int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
    Log.v(TAG,"Permission: "+permissions[0]+ " "+grantResults[0]);
}}

答案 2 :(得分:0)

您必须在清单中添加READ_EXTERNAL_STORAGE权限。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

从棉花糖及更高版本开始,您必须向用户要求Dangerous permission

read doc for requesting runtime permission