我无法共享资源中的音频文件。 每个应用程序都表示无法发送文件。
将输入流转换为临时文件的方法
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>
中标签。
然后您需要 XML 文件 file_paths.xml
在Android子目录res/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);
但是在能够像这样使用它之前我需要做的是:我需要将所有 Assets 复制到私有(private)文件目录,因为 FileProvider
本身没有访问您的 Assets 的选项(我想您可以通过自定义 ContentProvider
来实现此目的,但我发现方法很复杂并且没有那么多时间)。
参见this Android Developer reference on FileProvider了解更多信息。
我的简单解决方案如下所示:
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 音频播放器控件?这是我在制作视频之前所做的。
关于java - Android从 Assets 文件夹共享音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40671626/