繁体   English   中英

android从资源文件夹共享音频文件

[英]android share audio file from assets folder

我无法从资产中共享音频文件。 每个应用程序都说它无法发送文件。

将输入流转换为临时文件的方法

    public File getFile(String Prefix, String Suffix) throws IOException {

    File tempFile = File.createTempFile(Prefix, Suffix);
    AssetFileDescriptor tempafd = FXActivity.getInstance().getAssets().openFd(filepath);
    tempFile.deleteOnExit();
    FileOutputStream out = new FileOutputStream(tempFile);
    IOUtils.copy(tempafd.createInputStream(), out);


    return tempFile;
}

共享文件

        item2.setOnAction(n ->{
            try {
                Uri uri = Uri.fromFile(tekst.getFile(tekst.getFilename(), ".mp3"));
                Intent share = new Intent();
                share.setType("audio/*");
                share.setAction(Intent.ACTION_SEND);
                share.putExtra(Intent.EXTRA_STREAM, uri);
                FXActivity.getInstance().startActivity(share);
            } catch (IOException ex) {
                Logger.getLogger(MainCategoryCreator.class.getName()).log(Level.SEVERE, null, ex);
            }

        });

就在它发生的时候,我几乎遇到了同样的问题:我需要共享一个视频文件。 问题是:现在有共享内部文件的方法。 决不。 你需要一个ContentProvider ,或者因为它有点简单,它的扩展名是FileProvider

首先:您需要更新AndroidManifest.xml

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="my.package.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

这需要添加到<application>标记中。

然后你需要Android子目录res/xml/的XML文件file_paths.xml

它应该是这样的:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="objects" path="objects/"/>
</paths>

最后触发它,我需要这样称呼它:

Uri uri = Uri.parse("content://my.package.fileprovider/" + fn); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); // or parse uri each time
intent.setDataAndType(uri, "video/*"); // all video type == * - alternative: mp4, ...
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_GRANT_READ_URI_PERMISSION);
List<ResolveInfo> resInfoList = FXActivity.getInstance().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
    String packageName = resolveInfo.activityInfo.packageName;
    FXActivity.getInstance().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
FXActivity.getInstance().startActivity(intent);

但是我之前需要做的就是这样:我需要将所有资源复制到私有文件目录,因为FileProvider本身没有选项来访问你的资产(我想你可以通过自定义ContentProvider来实现这一点) ,但我找到了复杂的方法,并没有那么多时间)。

有关mor信息,请参阅FileProvider上的此Android开发人员参考

我的简单解决方案如下所示:

public boolean copyAssetsToStorage() throws NativeServiceException {
    try {
        String[] assets = getContext().getAssets().list(DIR_NAME);
        if (assets == null || assets.length == 0) {
            LOG.warning("No assets found in '" + DIR_NAME + "'!");
            return false;
        }
        File filesDir = getContext().getFilesDir();
        File targetDir = new File(filesDir, DIR_NAME);
        if (!targetDir.isDirectory()) {
            boolean b = targetDir.mkdir();
            if (!b) {
                LOG.warning("could not create private directory with the name '" + DIR_NAME + "'!");
                return false;
            }
        }
        for (String asset : assets) {
            File targetFile = new File(targetDir, asset);
            if (targetFile.isFile()) {
                LOG.info("Asset " + asset + " already present. Nothing to do.");
                continue;
            } else {
                LOG.info("Copying asset " + asset + " to private files.");
            }
            InputStream is = null;
            OutputStream os = null;
            try {
                is = getContext().getAssets().open(DIR_NAME + "/" + asset);
                os = new FileOutputStream(targetFile.getAbsolutePath());
                byte[] buff = new byte[1024];
                int len;
                while ((len = is.read(buff)) > 0)
                    os.write(buff, 0, len);
            } catch (IOException e) {
                LOG.log(Level.SEVERE, e.getMessage(), e);
                continue;
            }
            if (os != null) {
                os.flush();
                os.close();
            }
            if (is != null)
                is.close();
        }
        return true;
    } catch (IOException e) {
        LOG.log(Level.SEVERE, e.getMessage(), e);
        return false;
    }
}

如你所见,我现在只支持平面层次结构......

这至少对我有用。

问候,丹尼尔


另外一个问题 :为什么要发送Intent而不实现简单的JavaFX音频播放器控件? 这就是我在视频之前所做的事情。

暂无
暂无

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

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