2

I am stuck with a small project of mine. I want to overwrite the current lock screen wallpaper in order to change its image each time I lock the phone. I am using android studio for it with API 23. My phone has Android 6.0.1 and is rooted. The code works to copy a file to a non-root directory though.

My problem is, that when trying to copy the file to data/data/... I get

E/tag: /data/data/com.sec.android.wallpapercropper2/files/wallpaper.png: open failed: EACCES (Permission denied)

I wonder if there is an easy way to give my app superuser permission for that task. The only thing I found while searching was how to do it with shell commands, but I want to do it with java code if possible. Thanks a lot!

public class MainActivity extends AppCompatActivity {

// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    verifyStoragePermissions(this);
    copyFile("/storage/emulated/0/Download/", "wallpaper.png", "/data/data/com.sec.android.wallpapercropper2/files/");
}

private void copyFile(String inputPath, String inputFile, String outputPath) {
    InputStream in = null;
    OutputStream out = null;
    try {
        in = new FileInputStream(inputPath + inputFile);
        out = new FileOutputStream(outputPath + inputFile);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;

        // write the output file (You have now copied the file)
        out.flush();
        out.close();
        out = null;

    } catch (FileNotFoundException fnfe1) {
        Log.e("tag", fnfe1.getMessage());
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }
}
Bradley Wilson
  • 1,197
  • 1
  • 13
  • 26
Wolfgang Deigele
  • 85
  • 1
  • 1
  • 7

1 Answers1

0

I managed to solve it with the help of rootTools

CommandCapture command = new CommandCapture(0, "cp -f " + old_file + " " + new_file);
    try {
    RootTools.getShell(true).add(command);
    } catch(IOException io){}
    catch(TimeoutException ti){}
    catch(RootDeniedException rd){}
Wolfgang Deigele
  • 85
  • 1
  • 1
  • 7