È possibile condividere un file dall'archivio locale delle app a un'altra applicazione (come un allegato di posta elettronica) concedendo autorizzazioni temporanee per leggere tale file come parte della condivisione intento.
Passaggio 1: Aggiungere un provider di file sul AndroidManifest.xml:
<applicaton>
....
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.your.package.name.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
Fase 2: Aggiungi un res di file/xml/filepaths.xml con il percorso del file nella memoria applicazione locale che si desidera per condividere:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="myFolder" path="Folder/"/>
</paths>
Fase 3: Nel codice Java creare il file sharing intento:
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Your subject");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Your message");
ArrayList<Uri> uris = new ArrayList<Uri>();
String shareName = new String(pathToFile + filename);
File shareFile = new File(shareName);
Uri contentUri = FileProvider.getUriForFile(context, "com.your.package.name.fileprovider", shareFile);
uris.add(contentUri);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
// Grant temporary read permission to the content URI
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String msgStr = "Share...";
startActivity(Intent.createChooser(shareIntent, msgStr));
Se y Per eventuali problemi, consultare i documenti qui https://developer.android.com/training/secure-file-sharing/share-file.html per ulteriori dettagli.
http: // stackoverflow.it/questions/587917/trying-to-attach-a-file-from-sd-card-to-email – KOTIOS
è possibile vedere il file all'e-mail? – user1140237
@ Stacks28. Il problema è che sto attaccando dalla memoria interna. SD non mi aiuta – Snake