'Share file .txt Android
I'm trying to share a .txt file to another app (gmail for example) but I have some problems. I decided to use the file provider. When I select gmail among the apps to share the file with, the app returns me the "Couldn't attach file" toast.
I write the file txt in internal memory with this method:
public void esportaTxt(Graph graph, int id){
final String FILE_NAME = id + "_TXT" + ".txt";
List<Zona> zone = new ArrayList<>();
Iterator<Zona> iterator = graph.vertexSet().iterator();
zone = fromIteratorToArrayZone(iterator);
File fileOutputFolder = new File(context.getFilesDir(), "fileOutput"); //cartella in cui salvare i file da condividere
FileOutputStream fileOutputStream = null;
try {
fileOutputFolder.mkdirs(); //crea la cartella se non esiste
File file = new File(fileOutputFolder, FILE_NAME); //il file da salvare
fileOutputStream = new FileOutputStream(file);
for(int i = 0; i < zone.size(); i++){
fileOutputStream.write((i + 1 + ") " + zone.get(i).getNome() + "\n").getBytes());
Iterator<Oggetto> iteratoreOggetti = zone.get(i).getListaOggetti().iterator();
while (iteratoreOggetti.hasNext()){
fileOutputStream.write((" - " + iteratoreOggetti.next().getNome() + "\n").getBytes());
}
}
contentUri = FileProvider.getUriForFile(context, "com.example.eculturetool.fileprovider", file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I share the file txt with the following method:
public void shareFileTxt(int id){
String fileName = id + "_TXT.txt";
String stringFile = context.getFilesDir() + "/fileOutput" + File.separator + fileName;
File file = new File(stringFile);
contentUri = FileProvider.getUriForFile(context, "com.example.eculturetool.fileprovider", file);
if(!file.exists()){
Toast.makeText(context, "Il file non esiste!", Toast.LENGTH_LONG).show();
return;
}
Intent intentShare = new Intent(Intent.ACTION_SEND);
intentShare.setType("text/*");
intentShare.putExtra(Intent.EXTRA_SUBJECT, "Subject Here"); //per condividere con email app
intentShare.putExtra(Intent.EXTRA_STREAM, contentUri);
intentShare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(intentShare, "Condividi file"));
}
The permissions that I require in the Activity:
ActivityCompat.requestPermissions(this, new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, PERMISSION_GRANTED);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Provider in android manifest:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.eculturetool.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
The screen of Gmail
Solution 1:[1]
you must grant uri permission before starting intentShare , like this code:
context.grantUriPermission(
"your.package.name",
contentUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mahdi Iranmanesh |