i cannot understand about sharing internal txt file. (not external !) I noticed that is not possibile by default, but i writed my ContetProvider class
< provider android:name="myProvider" android:authorities="com.mobilemerit.usbhost" android:exported="true" />
public class myProvider extends ContentProvider {
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
File cacheDir = getContext().getCacheDir();
File privateFile = new File(cacheDir, "file.txt");
return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_WRITE);
}
then
try {
InputStream is = c.getAssets().open("file.txt");
File cacheDir = c.getCacheDir();
File outFile = new File(cacheDir, "file.txt");
OutputStream os = new FileOutputStream(outFile.getAbsolutePath());
String content = "Hello Java Code Geeks";
byte[] buff = new byte[1024];
int len;
while ((len = is.read(buff)) > 0) {
os.write(buff, 0, len);
}
os.flush();
os.close();
is.close();
} catch (IOException e) {
e.printStackTrace(); // TODO: should close streams properly here
}
Uri uri = Uri.parse("content://com.mobilemerit.usbhost/file.txt");
InputStream is = null;
StringBuilder result = new StringBuilder();
try {
is = c.getContentResolver().openInputStream(uri);
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = r.readLine()) != null) {
result.append(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try { if (is != null) is.close(); } catch (IOException e) { }
}
but the new sharing intent that i'm writing is attaching a file.txt that is not impossible to send. It seem the attachment "it's not valid". What am i wronging? I cannot use external memory
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://com.mobilemerit.usbhost/file.txt"));
startActivity(Intent.createChooser(intent, ""));