【问题标题】:How do I attach Ormlite database file to email intent in Android如何在 Android 中将 Ormlite 数据库文件附加到电子邮件意图
【发布时间】:2015-12-10 21:31:02
【问题描述】:

我正在维护一个使用 OrmLiteSqliteOpenHelper 在设备上创建 Sqlite 数据库的旧版 Android 应用。在设备上,我发现这个文件位于:

/data/data/[包名]/databases/mydatabase.db

我的应用程序具有“电子邮件支持”功能,不幸的是,我的任务是将此 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”作为我的邮件客户端时,我在电子邮件客户端上收到一个提示“附件的权限被拒绝”。

为什么会这样?如何授予 Gmail 对此文件的权限?任何帮助表示赞赏。谢谢。

【问题讨论】:

    标签: android sqlite email android-intent ormlite


    【解决方案1】:

    我已经找到了解决方法,但我仍然愿意接受其他解决方案。

    如果我使用 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 2016-12-29
      • 2012-11-19
      • 2015-03-06
      相关资源
      最近更新 更多