Android 为什么我无法发送应用程序中存储的文本文件';什么是内部存储?
我有一个App say(appa),其中我通过代码在内部存储器(data/data/com.example.mockapp/files)中存储了一个文本文件(testfile.txt)Android 为什么我无法发送应用程序中存储的文本文件';什么是内部存储?,android,android-intent,android-fileprovider,Android,Android Intent,Android Fileprovider,我有一个App say(appa),其中我通过代码在内部存储器(data/data/com.example.mockapp/files)中存储了一个文本文件(testfile.txt) // Create a file in the Internal Storage String content = "hello world"; String filename="testfile.txt"; File file = null; Fi
// Create a file in the Internal Storage
String content = "hello world";
String filename="testfile.txt";
File file = null;
FileOutputStream outputStream;
try {
file = new File(getFilesDir(), filename);
outputStream = new FileOutputStream(file);
outputStream.write(content.getBytes());
outputStream.close();
} catch (IOException e) {
Log.d("InternalfileException", "we are here in exception");
e.printStackTrace();
}
已确认文件已成功创建并存在(我已检查)
注意:使用文件提供程序共享文本文件
//XML
现在,当我启动应用程序时,会弹出一个选择器对话框,其中包含watsapp、gmail、message等选项,但
当我点击其中任何一个时,它只会显示
1) 在watsapp的情况下--显示txt文件,但它从未完成上载
2) 在gmail的情况下——显示的是从未作为附件附加的txt文件,发送的是空邮件
我错过了什么
这让我到现在都发疯了
请帮助..解决与
文件权限有关的问题。
尝试提供适当的读取
写入
权限,以使其正常工作。还要确保
在清单
中正确声明文件提供程序
谢谢 与文件权限有关的,将其整理出来。
尝试提供适当的读取
写入
权限,以使其正常工作。还要确保
在清单
中正确声明文件提供程序
谢谢 我遇到了同样的问题,最后我在本页找到了解决方案:
这是对我有用的代码。它是在Kotlin中完成的,但非常相似:
//name of document
val nombre = getString(R.string.app_name).replace(" ", "_")
//saving document in internal storage
contexto!!.openFileOutput("$nombre.txt", Context.MODE_PRIVATE).use {
it.write("holita".toByteArray())
}
//Generation Uri
val path = FileProvider.getUriForFile(
contexto!!,
"${BuildConfig.APPLICATION_ID}.provider",
File(contexto!!.filesDir, "My_Password_Saver.txt")
)
//Sharing document
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
putExtra(Intent.EXTRA_STREAM, path)
type = "text/plain"
}
startActivity(Intent.createChooser(shareIntent, "Enviar"))
我遇到了同样的问题,最后我在本页中找到了解决方案:
这是对我有用的代码。它是在Kotlin中完成的,但非常相似:
//name of document
val nombre = getString(R.string.app_name).replace(" ", "_")
//saving document in internal storage
contexto!!.openFileOutput("$nombre.txt", Context.MODE_PRIVATE).use {
it.write("holita".toByteArray())
}
//Generation Uri
val path = FileProvider.getUriForFile(
contexto!!,
"${BuildConfig.APPLICATION_ID}.provider",
File(contexto!!.filesDir, "My_Password_Saver.txt")
)
//Sharing document
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
putExtra(Intent.EXTRA_STREAM, path)
type = "text/plain"
}
startActivity(Intent.createChooser(shareIntent, "Enviar"))
请阅读此处有关获取正确URI和授予临时权限的部分:如果您在API级别15或更低级别上运行。还有,帮助处理一些有缺陷的客户端。@KenWolf,intent.addFlags(intent.FLAG\u GRANT\u READ\u URI\u PERMISSION);这还不够。?嗯,API级别20是Android 4.4W,我真的怀疑您是否在第一代Android Wear设备上运行此代码。@Commonware,对不起,我的不好,API级别是16。请阅读此处有关获取正确URI和授予临时权限的部分:如果您运行的API级别为15或更低。还有,帮助处理一些有缺陷的客户端。@KenWolf,intent.addFlags(intent.FLAG\u GRANT\u READ\u URI\u PERMISSION);这还不够。?嗯,API级别20是Android 4.4W,我真的怀疑您是否在第一代Android Wear设备上运行此代码。@Commonware,对不起,我的不好,API级别是16。我也面临同样的问题,你能帮我一下吗?我也面临同样的问题,你能帮我一下吗
Uri uri = FileProvider.getUriForFile(this, "com.example.mock.fileprovider", file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, null));
//name of document
val nombre = getString(R.string.app_name).replace(" ", "_")
//saving document in internal storage
contexto!!.openFileOutput("$nombre.txt", Context.MODE_PRIVATE).use {
it.write("holita".toByteArray())
}
//Generation Uri
val path = FileProvider.getUriForFile(
contexto!!,
"${BuildConfig.APPLICATION_ID}.provider",
File(contexto!!.filesDir, "My_Password_Saver.txt")
)
//Sharing document
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
putExtra(Intent.EXTRA_STREAM, path)
type = "text/plain"
}
startActivity(Intent.createChooser(shareIntent, "Enviar"))