如何将我的存储空间从数据/数据更改为Android /数据

时间:2016-06-12 17:30:14

标签: android database local-storage storage

我是菜鸟。安装时我的app文件夹的当前位置是数据/数据,但是我想将文件夹位置(com.time.notes)更改为Android / data,这是我安装的应用程序的文件夹中显示的很多,这是代码目前正在尝试。

   public void backup(String item) {
    OutputStream myOutput = null;
    InputStream myInput = null;
    File directory;
    String databasePath = "/data/data/"+getPackageName()+"/databases/Memo.db";
    File f = new File(databasePath);

    if (f.exists()) {
        try {

            directory = new File("/mnt/sdcard/"+getResources().getString(R.string.app_name)+"/Backup");
            if (!directory.exists())
                directory.mkdir();
            if (item.equals("Backup")) {

                myOutput = new FileOutputStream(directory.getAbsolutePath() + File.separator + "/"+getResources().getString(R.string.app_name));

                myInput = new FileInputStream(databasePath);
                Toast.makeText(this, "Backup Location " + directory, Toast.LENGTH_LONG).show();
            } else if (item.equals("Restore")) {
                myInput = new FileInputStream(directory.getAbsolutePath() + File.separator + "/"+getResources().getString(R.string.app_name));

                myOutput = new FileOutputStream(databasePath);
                Toast.makeText(this, "Backup Restored Successfully ", Toast.LENGTH_LONG).show();

            }
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();
        } catch (Exception e) {
        } finally {
            try {
                if (myOutput != null) {
                    myOutput.close();
                    myOutput = null;
                }
                if (myInput != null) {
                    myInput.close();
                    myInput = null;
                }
            } catch (Exception e) {
            }
        }
    }
}

0 个答案:

没有答案