简体   繁体   English

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

[英]Sharing a file with Android Intent: File extension is removed

I'm using the following code to share an audio file with Android Studio 3.3.2 and Java. 我正在使用以下代码与Android Studio 3.3.2和Java共享音频文件。 The file is extracted from the /raw/ folder. 该文件是从/ 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"));
}

In principle the sharing works fine, but the file is sent without a file extension, which of course makes it unreadable by most apps. 原则上,共享工作正常,但文件没有文件扩展名发送,这当然使大多数应用程序无法读取。 If I add the file extension manually (for example after downloading it from the e-mail client), the MP3 works fine. 如果我手动添加文件扩展名(例如从电子邮件客户端下载后),MP3工作正常。

"file" is fed in by another function and corresponds to the file name from the raw folder. “file”由另一个函数提供,并对应于raw文件夹中的文件名。 It is also found basically, otherwise the sharing process would not work at all. 它基本上也被发现,否则共享过程根本不起作用。

So, how can I share a file with retention of the file extension? 那么,如何共享保留文件扩展名的文件? Thanks for your help! 谢谢你的帮助!

Update #1 更新#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();
    }

}

I'm using the following code to share an audio file with Android Studio 3.3.2 and Java 我正在使用以下代码与Android Studio 3.3.2和Java共享音频文件

You are sharing a resource. 您正在共享资源。 That is a file on your development machine. 这是您的开发计算机上的文件。 It is not a file on the device. 它不是设备上的文件。

In principle the sharing works fine, but the file is sent without a file extension, which of course makes it unreadable by most apps. 原则上,共享工作正常,但文件没有文件扩展名发送,这当然使大多数应用程序无法读取。

In Android, resources do not have file extensions when we work with them using the Android SDK. 在Android中,当我们使用Android SDK处理资源时,资源没有文件扩展名。

So, how can I share a file with retention of the file extension? 那么,如何共享保留文件扩展名的文件?

If you share an actual file , using FileProvider , it will have a file extension. 如果您使用FileProvider共享实际文件 ,则它将具有文件扩展名。 So, copy the bytes corresponding to your resource to a file (eg, in getCacheDir() ), then set up FileProvider and use FileProvider.getUriForFile() to get the Uri to use with EXTRA_STREAM . 因此,将与资源相对应的字节复制到文件中(例如,在getCacheDir() ),然后设置FileProvider并使用FileProvider.getUriForFile()来使UriEXTRA_STREAM一起使用。

I worked out the solution! 我找到了解决方案!

MainActivity.java MainActivity.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 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 file_provider_paths.xml

<paths>
    <cache-path name="cache" path="/" />
    <files-path name="files" path="/" />
</paths>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM