我不能分享资产的音频文件。每个应用程序都说它不能发送文件。
将输入流转换为临时文件的方法。
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);
}
});发布于 2016-12-02 12:49:48
碰巧,我遇到了一个几乎相同的问题:我需要共享一个视频文件。问题是:现在有方法共享内部文件。绝不可能。您要么需要一个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子目录file_paths.xml中的XML文件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);但是,在能够这样使用它之前,我需要做的是:我需要将所有的资产复制到私有文件dir中,因为FileProvider本身没有访问您的资产的选项(我想您可以使用自定义的ContentProvider来实现这一点,但是我发现它太复杂了,没有那么多时间)。
有关mor信息,请参见这个Android开发者在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;
}
}正如你所看到的,我现在只支持一个扁平的层次.
这至少对我起了作用。
你好,丹尼尔
ADDIDIONAL问题:为什么要发送一个意图而不实现一个简单的JavaFX音频播放器控件?这是我在录影带之前做的事。
https://stackoverflow.com/questions/40671626
复制相似问题