Android 如何通过编程将短信数据库从根手机复制到sd卡
我有一部手机。 我想复制数据/data/com.android.providers.telephony/databases/mmssms.db 以编程方式将文件保存到sd卡Android 如何通过编程将短信数据库从根手机复制到sd卡,android,Android,我有一部手机。 我想复制数据/data/com.android.providers.telephony/databases/mmssms.db 以编程方式将文件保存到sd卡 File sd = Environment.getExternalStorageDirectory(); String path = Environment.getDataDirectory().getAbsolutePath(); File data
File sd = Environment.getExternalStorageDirectory();
String path = Environment.getDataDirectory().getAbsolutePath();
File data = Environment.getDataDirectory();
String currentDBPath = "/data/com.android.providers.telephony/databases/mmssms.db";
String backupDBPath = "/bkup/mmssms.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
// Local database
InputStream input = new FileInputStream(currentDB);
// Path to the external backup
OutputStream output = new FileOutputStream(backupDB);
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}`
我犯了这样的错误
java.io.FileNotFoundException:/data/data/com.android.providers.telephony/databases/mmssms.db(权限被拒绝)
File sd = Environment.getExternalStorageDirectory();
String path = Environment.getDataDirectory().getAbsolutePath();
File data = Environment.getDataDirectory();
String currentDBPath = "/data/com.android.providers.telephony/databases/mmssms.db";
String backupDBPath = "/bkup/mmssms.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
// Local database
InputStream input = new FileInputStream(currentDB);
// Path to the external backup
OutputStream output = new FileOutputStream(backupDB);
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}`
请帮帮我
File sd = Environment.getExternalStorageDirectory();
String path = Environment.getDataDirectory().getAbsolutePath();
File data = Environment.getDataDirectory();
String currentDBPath = "/data/com.android.providers.telephony/databases/mmssms.db";
String backupDBPath = "/bkup/mmssms.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
// Local database
InputStream input = new FileInputStream(currentDB);
// Path to the external backup
OutputStream output = new FileOutputStream(backupDB);
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}`
提前谢谢你
File sd = Environment.getExternalStorageDirectory();
String path = Environment.getDataDirectory().getAbsolutePath();
File data = Environment.getDataDirectory();
String currentDBPath = "/data/com.android.providers.telephony/databases/mmssms.db";
String backupDBPath = "/bkup/mmssms.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
// Local database
InputStream input = new FileInputStream(currentDB);
// Path to the external backup
OutputStream output = new FileOutputStream(backupDB);
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}`
我使用下面的代码复制sd卡中的数据库
'
试试{
File sd = Environment.getExternalStorageDirectory();
String path = Environment.getDataDirectory().getAbsolutePath();
File data = Environment.getDataDirectory();
String currentDBPath = "/data/com.android.providers.telephony/databases/mmssms.db";
String backupDBPath = "/bkup/mmssms.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
// Local database
InputStream input = new FileInputStream(currentDB);
// Path to the external backup
OutputStream output = new FileOutputStream(backupDB);
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}`
您正在尝试从其他应用程序复制数据库。这是不允许的。即使您的手机是根用户,您的应用程序也尚未被授予根用户权限
File sd = Environment.getExternalStorageDirectory();
String path = Environment.getDataDirectory().getAbsolutePath();
File data = Environment.getDataDirectory();
String currentDBPath = "/data/com.android.providers.telephony/databases/mmssms.db";
String backupDBPath = "/bkup/mmssms.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
// Local database
InputStream input = new FileInputStream(currentDB);
// Path to the external backup
OutputStream output = new FileOutputStream(backupDB);
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}`
String currentDBPath = "/data/com.android.providers.telephony/databases/mmssms.db";
您需要先授予root权限,然后使用shell命令复制文件,如(cp/data/…/xxx.db/sdcard/xxx.db)
File sd = Environment.getExternalStorageDirectory();
String path = Environment.getDataDirectory().getAbsolutePath();
File data = Environment.getDataDirectory();
String currentDBPath = "/data/com.android.providers.telephony/databases/mmssms.db";
String backupDBPath = "/bkup/mmssms.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
// Local database
InputStream input = new FileInputStream(currentDB);
// Path to the external backup
OutputStream output = new FileOutputStream(backupDB);
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}`
File sd = Environment.getExternalStorageDirectory();
String path = Environment.getDataDirectory().getAbsolutePath();
File data = Environment.getDataDirectory();
String currentDBPath = "/data/com.android.providers.telephony/databases/mmssms.db";
String backupDBPath = "/bkup/mmssms.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
// Local database
InputStream input = new FileInputStream(currentDB);
// Path to the external backup
OutputStream output = new FileOutputStream(backupDB);
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}`
只需使用本文中的方法并将shell命令更改为
File sd = Environment.getExternalStorageDirectory();
String path = Environment.getDataDirectory().getAbsolutePath();
File data = Environment.getDataDirectory();
String currentDBPath = "/data/com.android.providers.telephony/databases/mmssms.db";
String backupDBPath = "/bkup/mmssms.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
// Local database
InputStream input = new FileInputStream(currentDB);
// Path to the external backup
OutputStream output = new FileOutputStream(backupDB);
// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}`
stdin.writeBytes("cp /data/data/com.android.providers.telephony/databases/mmssms.db /sdcard/\n"); // \n executes the command
谢谢你的快速回复。我是这一部分的新手,所以我可以通过编程来完成吗?我提到的路径是我手机中短信数据库的实际路径。我自己做的:D,方法如上所述。是的!!!!!谢谢老兄…你是gr8。我让它工作了…谢谢你的帮助…朋友们好,我正在将相同的数据库恢复到/data/data/com.android.providers.telephony/databases/mmssms.db文件夹。还原已成功完成,但如果我打开消息传递应用程序,则会显示错误,如应用程序消息传递过程。com.android.mms已意外停止并强制关闭,请帮助我