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. To do this, I need to write the file to internal storage and then get the URI to it. 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):
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. 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:
<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.