[英]Android java write file internal memory and share
i cannot understand about sharing internal txt file. 我不了解共享内部txt文件的信息。 (not external !) I noticed that is not possibile by default, but i writed my ContetProvider class
(不是外部的!)我注意到默认情况下这不是可能的,但是我写了ContetProvider类
< provider android:name="myProvider" android:authorities="com.mobilemerit.usbhost" android:exported="true" />
<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. 但是我正在写的新共享意图是附加一个并非不可能发送的file.txt。 It seem the attachment "it's not valid".
附件似乎“无效”。 What am i wronging?
我在冤wrong吗? 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, ""));
If the only intention is to share/email an internal file, it isn't necessary to create a content provider. 如果唯一的目的是共享/通过电子邮件发送内部文件,则无需创建内容提供程序。 You can use
setReadable
( 1 ) method to let every process access the file. 您可以使用
setReadable
( 1 )方法来让每个进程访问文件。 Sample below. 下面的示例。
//open existing file
File emailFile= new File( getFilesDir(), "sampleFile.txt" );
//allow read for others
emailFile.setReadable( true, false );
Uri emailFileUri = Uri.fromFile( emailFile );
Intent intentToEmailFile = new Intent( Intent.ACTION_SEND );
intentToEmailFile.setType( "message/rfc822" );
intentToEmailFile.putExtra(android.content.Intent.EXTRA_STREAM, emailFileUri );
Intent chooserIntent = Intent.createChooser( intentToEmailFile, "Send email" );
startActivity( chooserIntent );
That will attach an internal file correctly to the email client of your choice. 这样会将内部文件正确地附加到您选择的电子邮件客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.