【问题标题】:How can I get my URI of .mp3 file stored in res/raw folder in android如何获取存储在 android 的 res/raw 文件夹中的 .mp3 文件的 URI
【发布时间】:2015-03-08 16:34:43
【问题描述】:

我有问题。我想将我的 MP3 文件分享到 Whatsapp,但它不起作用!这是我的分享意图:

    public void shareAchieve() {
        Intent shareAchievement = new Intent();
        shareAchievement.setType("audio/*");
        shareAchievement.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://de.logtainment.ungesoundboard/" + R.raw.achieve + ".mp3"));
        startActivity(Intent.createChooser(shareAchievement, "Teile Längstes Achievement"));
    }

但它不起作用!抱歉我的英语不好,我来自德国。

【问题讨论】:

  • 很少有应用程序支持android:resource 方案。如果 Whatapp 不支持它,那么您实际上无法强迫他们这样做。共享文件而不是资源会更好。

标签: android audio android-intent


【解决方案1】:

正如@CommonsWare 所说很少有应用支持android:resource 方案 使您的应用与所有应用兼容。

您必须通过此方法将原始资源复制到内部存储

private String CopyRAWtoSDCard(int raw_id,String sharePath) throws IOException {
        InputStream in = getResources().openRawResource(raw_id);
        FileOutputStream out = new FileOutputStream(sharePath);
        byte[] buff = new byte[1024];
        int read = 0;
        try {
            while ((read = in.read(buff)) > 0) {
                out.write(buff, 0, read);
            }
        } finally {
            in.close();
            out.close();
        }
        return sharePath;
    }

用下面的代码替换你的方法

  public void shareAchieve() {
    Intent shareAchievement = new Intent(Intent.ACTION_SEND);
    shareAchievement.setType("audio/*");

    String sPath= null;
    try {
        sPath = CopyRAWtoSDCard(R.raw.filename, Environment.getExternalStorageDirectory()+"/filename.mp3");
    } catch (IOException e) {
        e.printStackTrace();
    }
    Uri uri = Uri.parse(sPath);

    shareAchievement.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    shareAchievement.putExtra(Intent.EXTRA_STREAM,uri);
    startActivity(Intent.createChooser(shareAchievement, "Teile Längstes Achievement"));
}

同时添加

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

【讨论】:

  • 您使用哪个电子邮件客户端发送电子邮件? Gmail 还是其他?你能告诉我吗?
  • 对于 WhatsApp 不同的代码将适用于 Gamil 不同的你正在寻找两者?
  • 我都在找
  • 在我的shareAchieve 中是sharePath 红色!这是什么? @Ishrat
  • 看到你的截图我做了一些修改,请看更新答案。
【解决方案2】:

您错过了将 /raw/ 添加到路径并更改您的 Intent 之类的操作

改变

Intent shareAchievement= new Intent();
shareAchievement.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://de.logtainment.ungesoundboard/" + R.raw.achieve + ".mp3"));

Intent shareAchievement= new Intent(Intent.ACTION_SEND);
shareAchievement.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://de.logtainment.ungesoundboard/raw/achieve.mp3"));

【讨论】:

  • 当我从 Gmail 向我发送邮件时。 Gamil 说附件无法发送!在 Whatsapp 也是。 @法希姆
  • Gmail 也说,无法发送附件! @法希姆
  • @logtainment 你是否尝试过建议的更改
  • 请尝试建议的更改并告诉我。根据我应用后的说法,它应该可以工作
  • 不,我试过了,它不起作用。 Gmail 无法发送附件@Fahim
猜你喜欢
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
  • 2014-10-05
  • 2012-06-24
  • 2023-03-03
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多