You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Android应用分享raw目录视频到WhatsApp出现null.bin文件问题

解决Android Raw目录视频分享到WhatsApp显示null.bin的问题

问题原因

WhatsApp无法直接识别android.resource://类型的Uri,必须使用FileProvider生成可共享Uri,同时需要把raw目录里的视频文件复制到应用可对外访问的存储路径。

解决步骤

1. 配置FileProvider

在AndroidManifest.xml的<application>标签内添加以下配置:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

2. 创建文件路径配置文件

在res/xml目录下新建file_paths.xml(无xml目录则手动创建),内容如下:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="shared_videos" path="videos/" />
</paths>

该配置指定应用外部文件目录下的videos文件夹为可共享路径。

3. 修改分享代码

先实现复制raw文件到外部目录的方法,再生成共享Uri进行分享:

private void shareVideo() {
    // 复制raw目录的视频到外部可访问路径
    File videoFile = copyRawVideoToExternalStorage(R.raw.video_name, "my_video.mp4");
    if (videoFile == null) {
        // 复制失败,自行处理错误逻辑
        return;
    }

    // 生成FileProvider的Uri
    Uri shareUri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", videoFile);

    // 创建分享Intent
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("video/mp4"); // 指定准确MIME类型,避免用*/*
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 授予读取权限
    shareIntent.putExtra(Intent.EXTRA_STREAM, shareUri);
    startActivity(Intent.createChooser(shareIntent, "分享视频"));
}

// 复制raw文件到应用外部文件目录的videos文件夹
private File copyRawVideoToExternalStorage(int rawResId, String fileName) {
    File videosDir = new File(getExternalFilesDir(null), "videos");
    if (!videosDir.exists()) {
        if (!videosDir.mkdirs()) {
            return null; // 创建目录失败
        }
    }

    File outputFile = new File(videosDir, fileName);
    if (outputFile.exists()) {
        return outputFile; // 文件已存在,直接返回
    }

    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        inputStream = getResources().openRawResource(rawResId);
        outputStream = new FileOutputStream(outputFile);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        return outputFile;
    } catch (IOException e) {
        e.printStackTrace();
        if (outputFile.exists()) {
            outputFile.delete();
        }
        return null;
    } finally {
        try {
            if (inputStream != null) inputStream.close();
            if (outputStream != null) outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

关键注意点

  • 必须指定准确的MIME类型(如video/mp4),不能用*/*,确保WhatsApp能正确识别文件类型。
  • 一定要添加Intent.FLAG_GRANT_READ_URI_PERMISSION,授予WhatsApp读取Uri的权限。
  • 复制文件时需处理目录创建、文件已存在等异常情况,避免程序崩溃。

内容的提问来源于stack exchange,提问作者Bhaskar Jyoti Dutta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 17:35:26