2015-06-09 2 views
1

Попытка отправить SQLite db по электронной почте. Электронная почта успешно отправляется, но с ней нет приложения. Мой код:Отправить по электронной почте файл db Android

Uri uri; 
if(android.os.Build.VERSION.SDK_INT >= 4.2) 
{ 
uri = Uri.fromFile (
    new File (
    getApplicationContext().getApplicationInfo().dataDir + 
    "/databases/"+ MainActivity.accounts.getUserName()+ "D"+"/Dentist.db" 
    ) 
); 
} else { 
uri = Uri.fromFile (
    new File (
    "/data/data/" + getApplicationContext().getPackageName() + 
    "/databases/"+MainActivity.accounts.getUserName()+ "D"+"/Dentist.db" 
) 
); 
} 

Intent shareIntent = new Intent(Intent.ACTION_SEND); 
shareIntent.setType("application/db"); 
shareIntent.putExtra(Intent.EXTRA_EMAIL,new String[] { "" }); 
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Test"); 
shareIntent.putExtra(Intent.EXTRA_TEXT, ""); 
shareIntent.putExtra(Intent.EXTRA_STREAM, uri); 
startActivity(shareIntent); 

ответ

3

Попробуйте этот код, вы должны пройти ваше имя БД и адрес электронной почты, на котором вы хотите доли дб файл.

@SuppressWarnings("resource") 
    public static void exportDatabse(Context ctx) { 
     File backupDB = null; 
     try { 
      File sd = Environment.getExternalStorageDirectory(); 
      File data = Environment.getDataDirectory(); 

      if (sd.canWrite()) { 
       String currentDBPath = "//data//" + ctx.getPackageName() 
         + "//databases//" + "Your_db_name" + ""; 
       File currentDB = new File(data, currentDBPath); 
       backupDB = new File(sd, "Your_db_name"); 

       if (currentDB.exists()) { 

        FileChannel src = new FileInputStream(currentDB) 
          .getChannel(); 
        FileChannel dst = new FileOutputStream(backupDB) 
          .getChannel(); 
        dst.transferFrom(src, 0, src.size()); 
        src.close(); 
        dst.close(); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
     emailIntent.setType("*/*"); 
     emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
       new String[] { "[email protected]" }); 

     Random r = new Random(); 

     emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
       "Local db " + r.nextInt()); 
     emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(backupDB)); 
     ctx.startActivity(Intent.createChooser(emailIntent, "Export database")); 
    } 

Совершено

1

Приложение, отправляющее электронное письмо, не имеет доступа к вашему db-файлу. Вы должны скопировать db-файл в каталог внешних файлов приложений, прежде чем присоединять его к электронному письму.

Вы можете получить путь к каталогу внешних-файлов с помощью

context.getExternalFilesDir(null) 
Смежные вопросы