【发布时间】:2019-04-29 03:51:55
【问题描述】:
当我单击项目时,我试图从存储中播放音频,由于某种原因,文件为存在()返回 false,我具有读写外部存储权限,并且文件在那里并且似乎没有损坏,我的文件为 .mp4 格式,我将作为音频类型播放。
这是我的权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
提供者:
<provider
android:authorities="com.aliaskarurakov.android.mycallrecorderdemo"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
这是我的方法:
public void playAudio(){
String path = Environment.getExternalStorageDirectory() + "/tempAudioFile/temp.mp4";
Log.d("path", "onClick: path = "+path);
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(path);
Log.i("", "onResponse: name "+file.getName());
if (file.exists()){
Log.i("", "onResponse: I EXIST");
} else {
Log.i("", "onResponse: dont exist?");
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(getUriForFile(context,"com.aliaskarurakov.android.mycallrecorderdemo",file), "audio/*");
context.startActivity(intent);
}
【问题讨论】:
-
mp4 是 video,而不是 audio 文件。
-
我知道我可以将它作为音频播放。
-
我猜你应该使用 ,
"video/*")而不是, "audio/*") -
已经试过了,同样的问题。
-
试试这个:
intent.setDataAndType(Uri.fromFile(file), "video/*");,而不是你更长的指令。
标签: android file file-exists media-type