我有一个应用程序,我得到的SQLite数据库文件,并通过电子邮件发送它。问题是,在某些设备上,我用Context.getDatabasePath(“dbname.db”).getPath()检索的filePath返回null。
对于特定的设备,我是否需要特殊的权限?或者是否有其他方法可以检索将在所有设备上工作的数据库文件路径。
这是我用来获取文件并发送它的代码:
public synchronized void sendDBMail(String subject, String body,
String sender, String recipients, final DBSender.OnCompletionListener onCompletionListener) throws Throwable {
// Create a default MimeMessage object.
final Message message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(sender));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
// Set Subject: header field
message.setSubject(subject);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Now set the actual message
messageBodyPart.setText(body);
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
File myDB = FileUtils.copyDBToSD(App.getContext(),BACKUP_DATABASES_DIR, DBHelper.DATABASE_NAME);
String filePath = myDB.getPath();
DataSource source = new FileDataSource(filePath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(DBHelper.DATABASE_NAME);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
}
public static File copyDBToSD(Context ctx, String sdCardPath, String dataBaseFileName) throws Throwable {
File dataBaseFile = ctx.getDatabasePath(dataBaseFileName);
File dataBaseCopy = FileUtils.createOrReadFileInSDCard(ctx,sdCardPath,dataBaseFileName);
return copyFile(dataBaseFile, dataBaseCopy);
}谢谢你的帮助!
发布于 2018-02-01 04:18:44
将您的DBHelper修改为:
1)包含一个类变量:-
private static String DBPATH;2)在超级调用add之后的构造函数中(假设Context是传递的context的变量名,其他相应地更改):-
DBPATH = context.getDatabasePath(DB_NAME).getPath();3)添加一个方法:-
public static String getDBPath() {
return DBPATH;
}4)然后使用:-
File dataBaseFile = new File(DBhelper.getDBPath());而不是
File dataBaseFile = ctx.getDatabasePath(dataBaseFileName);然后,您可以在copyDBToSD的签名中去掉上下文。
当然,这确实依赖于已实例化的DBhelper实例。它还假设问题是基于App.getContext()是问题的原因。
发布于 2018-02-01 00:23:34
我在我的旧应用中使用了这个:
private boolean expdb() {
//exporting database file
try {
String filename = ("database_backup_" + DbOpenHelper.DB_NAME);
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath() + "/yourAppName");
//create directory if not exist
if (!directory.isDirectory()) {
directory.mkdirs();
}
File data = Environment.getDataDirectory();
if (directory.canWrite()) {
String currentDBPath = "//data//" + getActivity().getPackageName() + "//databases//" + DbOpenHelper.DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(directory, filename);
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();
} else{
return false;}
} else {
return false;}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}https://stackoverflow.com/questions/48546742
复制相似问题