Android 如何将 Ormlite 数据库文件附加到电子邮件意图
How do I attach Ormlite database file to email intent in Android
我正在维护一个遗留 Android 应用程序,它使用 OrmLiteSqliteOpenHelper 在设备上创建一个 Sqlite 数据库。在设备上,我发现这个文件位于:
/data/data/[包名]/databases/mydatabase.db
我的应用程序具有 "E-mail Support" 功能,不幸的是,我的任务是将此 SQLite 文件附加到电子邮件 Intent 中以解决用户问题。我 运行 遇到了一些权限问题。这是我正在使用的代码:
public void email(String[] to, String subject ) {
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("*/*");
email.putExtra(android.content.Intent.EXTRA_EMAIL, to);
email.putExtra(android.content.Intent.EXTRA_SUBJECT, subject );
File file = activity.get().getApplicationContext().getDatabasePath("mydatabase.db");
if( file.exists() )
{
if( file.canRead() )
{
Uri uri = Uri.fromFile(file);
email.putExtra(Intent.EXTRA_STREAM, uri);
activity.get().startActivity(Intent.createChooser(email, "Email DB File"));
}
}
}
当我 运行 此代码并选择 "Gmail" 作为我的邮件客户端时,我在电子邮件客户端上祝酒说 "Permission denied for attachment"。
为什么会这样?如何授予 Gmail 对此文件的权限?任何帮助表示赞赏。谢谢
我已经找到解决方法,但我仍然愿意接受其他解决方案。
如果我使用 Environment.getExternalStorageDirectory() 在 SD 卡上创建文件的临时副本,则 Gmail 客户端有权读取该文件。邮件客户端似乎只有访问内部存储的权限问题。
File file = activity.get().getApplicationContext().getDatabasePath("mydatabase.db");
if( file.exists() && file.canRead() )
{
try {
//We need to make a local copy of the file to SDCard, so Gmail can use it
File destination = new File(Environment.getExternalStorageDirectory(), "database_copy.db");
this.copy(file, destination);
//Attach file and send
Uri uri = Uri.fromFile(destination);
email.putExtra(Intent.EXTRA_STREAM, uri);
activity.get().startActivity(Intent.createChooser(email, "Email DB File"));
}
catch(IOException ioe){
return;
}
}
....
//File copy routine
private void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
我正在维护一个遗留 Android 应用程序,它使用 OrmLiteSqliteOpenHelper 在设备上创建一个 Sqlite 数据库。在设备上,我发现这个文件位于:
/data/data/[包名]/databases/mydatabase.db
我的应用程序具有 "E-mail Support" 功能,不幸的是,我的任务是将此 SQLite 文件附加到电子邮件 Intent 中以解决用户问题。我 运行 遇到了一些权限问题。这是我正在使用的代码:
public void email(String[] to, String subject ) {
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("*/*");
email.putExtra(android.content.Intent.EXTRA_EMAIL, to);
email.putExtra(android.content.Intent.EXTRA_SUBJECT, subject );
File file = activity.get().getApplicationContext().getDatabasePath("mydatabase.db");
if( file.exists() )
{
if( file.canRead() )
{
Uri uri = Uri.fromFile(file);
email.putExtra(Intent.EXTRA_STREAM, uri);
activity.get().startActivity(Intent.createChooser(email, "Email DB File"));
}
}
}
当我 运行 此代码并选择 "Gmail" 作为我的邮件客户端时,我在电子邮件客户端上祝酒说 "Permission denied for attachment"。
为什么会这样?如何授予 Gmail 对此文件的权限?任何帮助表示赞赏。谢谢
我已经找到解决方法,但我仍然愿意接受其他解决方案。
如果我使用 Environment.getExternalStorageDirectory() 在 SD 卡上创建文件的临时副本,则 Gmail 客户端有权读取该文件。邮件客户端似乎只有访问内部存储的权限问题。
File file = activity.get().getApplicationContext().getDatabasePath("mydatabase.db");
if( file.exists() && file.canRead() )
{
try {
//We need to make a local copy of the file to SDCard, so Gmail can use it
File destination = new File(Environment.getExternalStorageDirectory(), "database_copy.db");
this.copy(file, destination);
//Attach file and send
Uri uri = Uri.fromFile(destination);
email.putExtra(Intent.EXTRA_STREAM, uri);
activity.get().startActivity(Intent.createChooser(email, "Email DB File"));
}
catch(IOException ioe){
return;
}
}
....
//File copy routine
private void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}