In my app, the user chooses a directory, and in onActivityResult, a DocumentFile is created like so:
Uri treeUri = resultData.getData();
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
DocumentFile newFile = pickedDir.createFile("text/plain", filename);
I then write to this DocumentFile with an OutputStream, use getUri() to get the Uri, and add this Uri as an extra to an email intent so that it is attached to the email.
Right now, due to the fact that I have chosen "text/plain" as my MIME type, the attached file has a ".txt" extension. However, I would like to change this extension to ".vtk", but I have not been able to find a solution anywhere. Is there a way to do this, or am I limited solely to the recognized MIME types of Android? ".vtk" is not recognized.
due to the fact that I have chosen "text/plain" as my MIME type, the attached file has a ".txt" extension
Not exactly. There is no requirement for any particular filename structure. After all, it is entirely possible to create a storage provider that does not work in terms of files (e.g., content is saved in BLOB columns in a database, locally or on a server).
How the email attachment will be named is based on the following:
content
Uri
will look like for your contentcontent
Uri
in EXTRA_STREAM
(which is what I assume you are using)Your filename
is your suggestion for what this thing might be called. You can try to put a file extension on there, but it might not be honored (the docs suggest that one should not be provided). Similarly, you can call renameTo()
on the DocumentFile
to change the name. Whether the storage provider honors any file extension involved in renameTo()
is up to the provider. The user can go into other apps that work with the storage provider and name it whatever the user wants. The email client can do whatever it wants for the attachment name. And so on.
I would like to change this extension to ".vtk", but I have not been able to find a solution anywhere
If you have put .vtk
on your filename
, that is all you can do. What happens from there is beyond your control.
am I limited solely to the recognized MIME types of Android?
You are welcome to replace text/plain
with any valid MIME type, though what the storage provider will do with this is up to the provider. In particular, I would not assume that any provider will know that such-and-so MIME type ought to have a .vtk
file extension. I'm not sure that there is a standard MIME type that resolves to a .vtk
file extension.
".vtk" is not recognized.
That is not a MIME type, if that is what you mean.