1

这是我的代码:

try 
        {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
            String es= System.getenv("EXTERNAL_STORAGE");

            if (sd.canWrite()) 
            {                                   
                String currentDBPath = "/data/com.my.package/databases/my.db";
                String backupDBPath = "my_db_backup.db3";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(es, backupDBPath);

                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();
                }
                Toast.makeText(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show();
            }               
        } 
        catch (Exception e) {
            Log.e("APP ERROR", "APP DB Backup", e);
        }

全部成功执行。EXTERNAL_STORAGE 路径是: storage/emulated/legacy 。但是当我使用 DDMS 时,EXTERNAL_STORAGE 路径在那里,但没有文件存在。我有一个HTC One...

更新:2013 年 11 月 1 日我尝试了以下所有建议,但就像我的代码一样,没有抛出任何错误,这让我相信它的手机只是默默地不允许我将任何内容复制到我的 SD 卡。我有一部 HTC 手机……在执行“将 db 复制到 sd 卡”代码之前,我需要对手机进行任何设置或程序吗?

4

3 回答 3

2

试试这个对我来说就像魅力一样

public void exportDatabse(String databaseName) {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
                String backupDBPath = "backupname.db";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                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) {

        }
    }

笔记 :

只需传递数据库名称而不指定扩展名。

像这样 :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                exportDatabse("my"); // Correct
                exportDatabse("my.db"); // Wrong
    } 
于 2013-10-31T04:29:54.053 回答
0

首先,您需要检查您是否已授予将文件写入清单文件中的 sdcard 的写入权限。

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

供参考检查此链接

谢谢

于 2013-10-31T04:25:05.590 回答
0

你能试试这个吗

void getDBBackUp(){
    File cacheDir;
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"NewFolder");
    else
        cacheDir = getCacheDir();
    if (!cacheDir.exists())
        cacheDir.mkdirs();

    Calendar c = Calendar.getInstance();
    String temp =""+c.get(Calendar.DATE) +"-"+ (c.get(Calendar.MONTH)+1) +"-"+ c.get(Calendar.YEAR) +"-"+ c.get(Calendar.HOUR)+"-"+ c.get(Calendar.MINUTE) +"-"+ c.get(Calendar.SECOND);
    String PACKAGE_NAME = getApplicationContext().getPackageName();

    try {
       File sd = Environment.getExternalStorageDirectory();
       File data = Environment.getDataDirectory();
       File dir = new File (sd.getAbsolutePath() + "/NewFolder");

       if (dir.canWrite()) {
           String currentDBPath = "//data//"+PACKAGE_NAME+"//databases//DatabaseName";
           String backupDBPath = "DB_"+temp;
           File currentDB = new File(data, currentDBPath);
           File backupDB = new File(dir, backupDBPath);

           AppLog.logString(TAG+"PACKAGE_NAME  : "+PACKAGE_NAME);
           AppLog.logString(TAG+"currentDBPath : "+currentDBPath);
           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();
           }
           AppLog.logString(TAG+"backupDB : "+backupDB);
           ErrorDialog.dispDialog(LogInActivity.this, "Backup Store At: "+backupDB, "eLitePOS");
       }
   } catch (Exception e) {
    e.printStackTrace();
   }
}

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-10-31T04:19:18.790 回答