1

アプリからSDカードにデータベースをバックアップしようとしていますが、毎回取得しようと FileNotFoundExceptionしていますが、データベースの名前が正しい理由がわかりません。

データベースをバックアップするためのコード

try{
        File dbFile = new File(Environment.getDataDirectory() + "/data/my.app.package/databases/Games.db");
        File exportDir = new File(Environment.getExternalStorageDirectory()+"/BCAData");
           if (!exportDir.exists()) {
              exportDir.mkdirs();
           }
           File file = new File(exportDir, dbFile.getName());

           file.createNewFile();
           FileChannel inChannel = new FileInputStream(dbFile).getChannel();  //fails here
           FileChannel outChannel = new FileOutputStream(file).getChannel();
           try {
              inChannel.transferTo(0, inChannel.size(), outChannel);
           } finally {
              if (inChannel != null)
                 inChannel.close();
              if (outChannel != null)
                 outChannel.close();
           }
    }catch(Exception e){
        e.printStackTrace();
    }

私のコンテンツプロバイダーでは、このようなデータベース名を持っています

private static final String DATABASE_NAME = "Games";
private static final String GAMES_TABLE = "Games";

エラー:

12-14 19:38:38.313: W/System.err(10452): java.io.FileNotFoundException: /data/data/com.tyczj.bowling/databases/Games.db: open failed: ENOENT (No such file or directory)
12-14 19:38:38.313: W/System.err(10452):    at libcore.io.IoBridge.open(IoBridge.java:416)
12-14 19:38:38.313: W/System.err(10452):    at java.io.FileInputStream.<init>(FileInputStream.java:78)
12-14 19:38:38.323: W/System.err(10452):    at com.tyczj.bowling.services.DatabaseExportService.onHandleIntent(DatabaseExportService.java:27)
12-14 19:38:38.323: W/System.err(10452):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
12-14 19:38:38.333: W/System.err(10452):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 19:38:38.333: W/System.err(10452):    at android.os.Looper.loop(Looper.java:137)
12-14 19:38:38.343: W/System.err(10452):    at android.os.HandlerThread.run(HandlerThread.java:60)
12-14 19:38:38.343: W/System.err(10452): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
12-14 19:38:38.353: W/System.err(10452):    at libcore.io.Posix.open(Native Method)
12-14 19:38:38.353: W/System.err(10452):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
12-14 19:38:38.353: W/System.err(10452):    at libcore.io.IoBridge.open(IoBridge.java:400)
12-14 19:38:38.353: W/System.err(10452):    ... 6 more

パスが間違っている理由がわかりません。データベースの名前ではありませんか?

4

1 に答える 1

4

それ以外の:

File dbFile = new File(Environment.getDataDirectory() + "/data/my.app.package/databases/Games.db");

試す:

File dbFile = getDatabasePath(DATABASE_NAME);
于 2012-12-15T00:46:23.107 に答える