簡體   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