[英]Get Uri from file I just created (android)
forgive me for what is probably a stupid question, but I really can't find the answer anywhere. 原谅我可能是一个愚蠢的问题,但我真的找不到任何答案。
I need my program to save the contents of an EditText as an html file and share it with other applications. 我需要我的程序将EditText的内容保存为html文件并与其他应用程序共享。 To do this, I need to write the file to internal storage and then get the URI to it.
为此,我需要将文件写入内部存储,然后获取URI。 However, there is no information anywhere (that I could fine) on how to do that.
但是,在任何地方(我都可以)没有关于如何做到这一点的信息。
This is my code (Editor being the name of the Activity and myapp the package): 这是我的代码(编辑器是Activity的名称,myapp是包的名称):
String filename = "Lorem ipsum.html";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(Html.toHtml(textField.getText()).getBytes());
outputStream.close();
System.out.println(filename);
} catch (Exception e) {
e.printStackTrace();
}
Intent shareIntent = ShareCompat.IntentBuilder.from(Editor.this)
.setStream(FileProvider.getUriForFile(Editor.this, "com.myapp.fileprovider", new File(filename)))
.getIntent();
shareIntent.setData(FileProvider.getUriForFile(Editor.this, "com.myapp.fileprovider", new File(filename)));
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(shareIntent);
As far as I can tell, the file creation part works just fine; 据我所知,文件创建部分工作得很好; no exception is caught.
没有例外被捕获。 However, the actual sharing part crashes with this error:
但是,实际共享部分崩溃时出现此错误:
java.lang.IllegalArgumentException: Failed to find configured root that contains /Lorem ipsum.html
I'm clearly doing something wrong when trying to find the URI, but I've been unable to find a solution. 我在尝试查找URI时显然做错了,但我一直无法找到解决方案。 Following the official documentation did not help and I could find no examples of this specific problem.
官方文档后没有帮助,我找不到这个具体问题的例子。
If it matters, here is my fileprovider declaration: 如果重要,这是我的fileprovider声明:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths" />
</provider>
Thank you in advance for any help. 预先感谢您的任何帮助。
In your code: 在你的代码中:
FileProvider.getUriForFile(Editor.this, "com.myapp.fileprovider", new File(filename))
The third parameter is equivalent to: 第三个参数相当于:
new File("Lorem ipsum.html"");
which is invalid. 这是无效的。
You should use the full path of the file as the parameter in the constructor of the File
. 您应该使用文件的完整路径作为
File
的构造函数中的参数。
As commented by @Fatih you should use 正如@Fatih评论你应该使用
new File(getFilesDir(), filename)
as the third parameter. 作为第三个参数。
Also your authorities
parameter in manifest should be corrected as: 此外,清单中的
authorities
参数应更正为:
android:authorities="${applicationId}.provider"
So your final statement will be: 所以你的最终陈述将是:
Uri uri= FileProvider.getUriForFile(Editor.this,
getApplicationContext().getPackageName() + ".provider",
new File(Editor.this.getFilesDir(), filename));
In res/xml folder paths file change: 在res / xml文件夹路径文件中更改:
<?xml version="1.0" encoding="utf-8"?> <paths> <files-path name="files" path="." /> </paths><!-- NOT THIS: <external-path name="external_files" path="."/>-->
In case if authorities parameter in manifest is : 如果清单中的权限参数是:
android:authorities="${applicationId}.provider"
And within the java code : 并在java代码中:
File file = new File(context.getFilesDir(), fileName);
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.