播放用户使用MediaPlayer指定的mp3文件时遇到问题。
try {
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(getApplicationContext(), audio);
mPlayer.prepare();
mPlayer.start(); }
catch (IOException e) {
numberText.setText("IO EXCEPTION");
e.printStackTrace();
}
问题在于mPlayer.setDataSource(getApplicationContext(),audio)
这里我从用户那里得到指定的mp3
setAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
audioPath = intent.getData().getPath();
audio = Uri.parse(audioPath);
}
}
}
});
错误日志显示此
setDataSource: SecurityException! uri=content://com.android.providers.downloads.documents/document/1545
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.downloads.DownloadStorageProvider from ProcessRecord{7b8354b 23595:com.example.m1tr0s0ul4s.thatawkwardcall/u0a137} (pid=23595, uid=10137) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
at android.os.Parcel.readException(Parcel.java:1540)
at android.os.Parcel.readException(Parcel.java:1493)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3372)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4819)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2527)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1486)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1092)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:932)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:859)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:987)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:961)
at com.example.m1tr0s0ul4s.thatawkwardcall.AcceptCall.onCreate(AcceptCall.java:55)
at android.app.Activity.performCreate(Activity.java:5966)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2517)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:189)
at android.app.ActivityThread.main(ActivityThread.java:5529)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
10-20 17:31:13.041 23595-23595/com.example.m1tr0s0ul4s.thatawkwardcall
E/MediaPlayer: Unable to create media player
我完全不知道发生了什么:/请帮助
答案 0 :(得分:0)
如果您已经请求运行时权限,请尝试以下代码:
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 1);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, 1);
}
如果您不这样做,则必须检查Marshmallow及以上版本的运行时权限。 将此代码添加到Manifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
现在在你的setAudio的onClick中你需要检查用户是否授予你访问设备存储的权限
setAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 11);
} else {
//You already have the permission, just go ahead.
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 1);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, 1);
}
}
}
当您请求权限时,将在 onRequestPermissionResult(..)中返回用户操作。以下是处理用户操作的方法。
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 11) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//The External Storage READ Permission is granted to you... Continue your left job...
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 1);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, 1);
}
} else{
//permission denied
}
}
答案 1 :(得分:0)
audioPath = intent.getData().getPath();
audio = Uri.parse(audioPath);
intent.getData()
已经返回Uri
个对象,因此您可能会丢弃读取特定文件所需的数据,如方案,主机或查询属性。