I am saving a file on internal storage. It is just a .txt file with some information about objects:
FileOutputStream outputStream;
String filename = "file.txt";
File cacheDir = context.getCacheDir();
File outFile = new File(cacheDir, filename);
outputStream = new FileOutputStream(outFile.getAbsolutePath());
outputStream.write(myString.getBytes());
outputStream.flush();
outputStream.close();
Then I am creating a "shareIntent" to share this file:
Uri notificationUri = Uri.parse("content://com.package.example/file.txt");
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, notificationUri);
shareIntent.setType("text/plain");
context.startActivity(Intent.createChooser(shareIntent, context.getResources().getText(R.string.chooser)));
The chosen app now needs access to the private file so I created a Content provider. I just changed the openFile method:
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
File privateFile = new File(getContext().getCacheDir(), uri.getPath());
return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY);
}
The manifest:
<provider
android:name=".ShareContentProvider"
android:authorities="com.package.example"
android:grantUriPermissions="true"
android:exported="true">
</provider>
When opening the Mail App to share the file it says, that it could not attach the file because it only has 0 Bytes. Sharing it via Bluetooth also failed. But I can read out the privateFile
in the Content Provider, so it exists and it has content. What is the problem?
query()
method called in your customContentProvider
? – Cavemanandroid.support.v4.content.FileProvider
? – CavemanContentProvider
seeandroid.provider.OpenableColumns
– CavemanContentProvider
as it is much more flexible thenFilePrivider
. – Labanthey are in the second argument.
And.. do you provide the requested info? – LabanMatrixCursor
if you dont know what to return fromquery
method – Cavemanflexible? All I need is the access to the private file
. Maybe today only. But later... With FileProvider you only can serve files from predetermined directories. Predetermind by Google. Not by you. – Labanopening the Mail App to share the file
. Which one? There mostly are more on a system. Try others too. – Laban