使用Android Intent共享文件:删除文件扩展名

问题描述 投票:0回答:2

我正在使用以下代码与Android Studio 3.3.2和Java共享音频文件。该文件是从/ raw /文件夹中提取的。

public void funktionTeilen(String file) {
    Uri uri = Uri.parse("android.resource://" + this.getPackageName() + "/raw/" + file);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("audio/mpeg3");
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Audio teilen"));
}

原则上,共享工作正常,但文件没有文件扩展名发送,这当然使大多数应用程序无法读取。如果我手动添加文件扩展名(例如从电子邮件客户端下载后),MP3工作正常。

“file”由另一个函数提供,并对应于raw文件夹中的文件名。它基本上也被发现,否则共享过程根本不起作用。

那么,如何共享保留文件扩展名的文件?谢谢你的帮助!

更新#1

public void funktionTeilen(String Datei) {


    try {
        File tmpFile = new File(this.getCacheDir() + "/tmpfile.mp3");
        InputStream in = getResources().openRawResource(R.raw.meise1);
        FileOutputStream out = new FileOutputStream(tmpFile, true);
        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();
        }

        // Uri uri = FileProvider.getUriForFile(this, this.getPackageName(), tmpFile);
        Uri uri = Uri.fromFile(tmpFile);
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("audio/*");
        share.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(share, "Audio teilen"));

    } catch (Exception e) {
        Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
    }

}
java android file audio sharing
2个回答
0
投票

我正在使用以下代码与Android Studio 3.3.2和Java共享音频文件

您正在共享资源。这是您的开发计算机上的文件。它不是设备上的文件。

原则上,共享工作正常,但文件没有文件扩展名发送,这当然使大多数应用程序无法读取。

在Android中,当我们使用Android SDK处理资源时,资源没有文件扩展名。

那么,如何共享保留文件扩展名的文件?

如果您使用FileProvider共享实际文件,它将具有文件扩展名。因此,将对应于您的资源的字节复制到文件(例如,在getCacheDir()中),然后设置FileProvider并使用FileProvider.getUriForFile()来获取Uri以与EXTRA_STREAM一起使用。


0
投票

我找到了解决方案!

main activity.Java

public void funktionTeilen(String Datei) {

    try {
        File tmpFile = new File(this.getCacheDir() + "/tmpfile.mp3");
        InputStream in = getResources().openRawResource(R.raw.meise1);
        FileOutputStream out = new FileOutputStream(tmpFile, false);
        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();
            /* if (tmpFile.exists()) {
                Toast.makeText(MainActivity.this, tmpFile.getAbsolutePath(), Toast.LENGTH_LONG).show();
            } */
        }

        Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, tmpFile.getAbsoluteFile());
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("audio/mpeg3");
        share.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(share, "Audio teilen"));

    } catch (Exception e) {
        Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
    }

}

AndroidManifest.xml中

<application>
        <provider
        android:name="android.support.v4.content.FileProvider"
        android:grantUriPermissions="true"
        android:exported="false"
        android:authorities="${applicationId}">

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_provider_paths"/>

    </provider>

</application>

file_provider_paths.xml

<paths>
    <cache-path name="cache" path="/" />
    <files-path name="files" path="/" />
</paths>
© www.soinside.com 2019 - 2024. All rights reserved.