【问题标题】:Open or play file with compatible apps from my android app从我的 android 应用程序中打开或播放兼容应用程序的文件
【发布时间】:2018-05-05 02:15:07
【问题描述】:

我已经在 Listview 中列出了 sdcard 中的所有文件,所以我想用兼容的已安装应用程序播放点击的文件。我尝试了所有相关问题及其正确答案,但是当我单击文件并选择兼容的应用程序播放时,我仍然遇到问题。选择的应用程序只打开而不播放点击的文件 这是我的代码:

    private void onFileClick(String path) {

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_VIEW);
    shareIntent.putExtra(Intent.EXTRA_STREAM, path);

    shareIntent.setData(Uri.fromFile(new File(path)));
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    // get file type
    String type;
    String extenstion = MimeTypeMap.getFileExtensionFromUrl(o.getPath());
    if (extenstion != null) {

        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extenstion);
        shareIntent.setType(type);
    } else {
        Toast.makeText(this, extenstion.toString(), Toast.LENGTH_LONG).show();
    }
    startActivity(Intent.createChooser(shareIntent, "Play with"));
}

任何帮助将不胜感激

【问题讨论】:

  • @pskink,@CommonsWare

标签: java android


【解决方案1】:

试试这个:

private void onFileClick(String path) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_VIEW);
    shareIntent.putExtra(Intent.EXTRA_STREAM, path);

    shareIntent.setData(Uri.fromFile(new File(path)));
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    // get file type
    String type;
    String extenstion = MimeTypeMap.getFileExtensionFromUrl(o.getPath());
    if (extenstion != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extenstion);
        shareIntent.setType(type);
    } else {
        Toast.makeText(this, extenstion.toString(), Toast.LENGTH_LONG).show();
    }
    startActivity(Intent.createChooser(shareIntent, "Play with"));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 2011-07-24
    相关资源
    最近更新 更多