【问题标题】:file.exists() returns false, can't play my audio filefile.exists() 返回 false,无法播放我的音频文件
【发布时间】: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);
    }

【问题讨论】:

  • mp4video,而不是 audio 文件。
  • 我知道我可以将它作为音频播放。
  • 我猜你应该使用 , "video/*") 而不是 , "audio/*")
  • 已经试过了,同样的问题。
  • 试试这个:intent.setDataAndType(Uri.fromFile(file), "video/*");,而不是你更长的指令。

标签: android file file-exists media-type


【解决方案1】:

您仍然需要在运行时请求许可。在需要的地方调用这个函数。

private void checkReadStoragePermission(){
    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                PERMISSION_REQUEST_READ_EXTENAL_STORAGE);
    }else{
        // Do your stuff here if permission is already granted by user
    }
}

然后处理用户是否同意。

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        // Here is the permission that you want
        case PERMISSION_REQUEST_READ_EXTENAL_STORAGE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                // Do your stuff here if permission is granted by user

    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多