Android Studio - 如果文件存在于系统文件夹中

时间:2015-11-28 10:23:19

标签: java android

这是我在java中的第一个项目。 我想在我的应用中添加壁纸备份/恢复功能。

备份代码:

public void btnBackupWallpaper(View view) {
    File wallpaper = new File("/data/system/users/0/wallpaper");
    if(wallpaper.exists()){
        RootCmd.RunRootCmd("cp -f /data/system/users/0/wallpaper /data/local/tmp/wallpaper");
        Toast.makeText(getApplicationContext(), "Wallpaper backup completed.",
                Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(getApplicationContext(), "Wallpaper not found.",
                Toast.LENGTH_LONG).show();
    }
}

要恢复的代码

public void btnRestoreWallpaper(View view) {
    File wallpaper = new File("/data/local/tmp/wallpaper");
    if(wallpaper.exists()){
        RootCmd.RunRootCmd("cp /data/local/tmp/wallpaper /data/system/users/0/wallpaper");
        RootCmd.RunRootCmd("chmod 0700 /data/system/users/0/wallpaper");
        RootCmd.RunRootCmd("chown system.system /data/system/users/0/wallpaper");
        RootCmd.RunRootCmd("rm /data/local/tmp/wallpaper");
        Toast.makeText(getApplicationContext(), "Wallpaper restore completed.",
                Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(getApplicationContext(), "Wallpaper backup not found.",
                Toast.LENGTH_LONG).show();
    }
}

恢复正常工作,但备份总是说"找不到壁纸"。

File wallpaper = new File("/data/system/users/0/wallpaper");
    if(wallpaper.exists()){}

为什么这部分代码不起作用?

感谢。

1 个答案:

答案 0 :(得分:0)

我的解决方案是使用RootTools:

public void btnBackupWallpaper(View view) {
    if(RootTools.exists("/data/system/users/0/wallpaper")) {
        RootCmd.RunRootCmd("cp -f /data/system/users/0/wallpaper /data/local/tmp/wallpaper");
        Context context = getApplicationContext();
        Toast.makeText(context, context.getString(R.string.wallpaper_backup_completed), Toast.LENGTH_LONG).show();
    } else {
        Context context = getApplicationContext();
        Toast.makeText(context, context.getString(R.string.wallpaper_not_found), Toast.LENGTH_LONG).show();
    }
}