为什么我无法发送存储在应用程序内部存储中的文本文件?
Why am I unable to send text file stored in my app's internal storage?
我有一个应用程序 say(app A),其中我通过代码在内部存储空间 (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;
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
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path path="." name="mockfiles" />
</paths>
//清单
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.mock.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
//这是主要 ACTIVITY 中用于发送 INTENT
的代码
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));
现在,当我启动应用程序时,它会弹出带有 watsapp、gmail、消息等选项的选择器对话框,但是
当我点击它们中的任何一个时,它只显示..
1) 对于 watsapp -- 显示 txt 文件但从未完成上传
2) 对于 gmail -- 显示从未作为附件附加的 txt 文件,发送空邮件。
我错过了什么?
这让我到现在都发疯了。
好心帮忙..
和file permissions
有关系,整理一下。
尝试提供适当的 read
write
权限,以使其正常工作。还要确保
您在 manifest
中正确声明了文件提供程序。
谢谢!
我遇到了同样的问题,最后我在这个页面找到了解决方案:
https://jayrambhia.com/blog/android-fileprovider-ecosystem
这是对我有用的代码。在 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"))
我有一个应用程序 say(app A),其中我通过代码在内部存储空间 (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;
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
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path path="." name="mockfiles" />
</paths>
//清单
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.mock.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
//这是主要 ACTIVITY 中用于发送 INTENT
的代码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));
现在,当我启动应用程序时,它会弹出带有 watsapp、gmail、消息等选项的选择器对话框,但是
当我点击它们中的任何一个时,它只显示..
1) 对于 watsapp -- 显示 txt 文件但从未完成上传
2) 对于 gmail -- 显示从未作为附件附加的 txt 文件,发送空邮件。
我错过了什么?
这让我到现在都发疯了。
好心帮忙..
和file permissions
有关系,整理一下。
尝试提供适当的 read
write
权限,以使其正常工作。还要确保
您在 manifest
中正确声明了文件提供程序。
谢谢!
我遇到了同样的问题,最后我在这个页面找到了解决方案: https://jayrambhia.com/blog/android-fileprovider-ecosystem
这是对我有用的代码。在 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"))