我创建了一个生成 JSON 文件并保存在/data/data/路径中的代码,我想获取所选文件并共享/导出。我正在尝试使用 Intent,请按照代码操作:
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
File fileItem = itens.get(position);
holder.textFileName.setText(fileItem.getName());
holder.shareImage.setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/json");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:/" + fileItem));
context.startActivity(Intent.createChooser(intent, "Compartilhar log de auditoria"));
});
}
但是,当我选择像文件管理器一样保存媒体并选择文件夹时,显示“路径不正确”,并且在所有应用程序中都显示附件不兼容。救命!
OBS:我正在RecyclerView类中实现代码
最佳答案
在 @CommonsWare 发表评论后,我使用了 FileProvider,现在我可以共享/导出 JSON 文件,请按照工作代码操作:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/json");
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, fileItem);
intent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(intent, "Exportar arquivo de Log de Auditoria"));
关于java - 如何在 Java Android 中共享/导出 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71931314/