Copy apk located at /data/app/ programmatically

Asked

Viewed 61 times

0

Hello, i would like to copy an apk located in the folder /data/app/.. But apk is not being copied, only the folder is created, follows the code:

InputStream input;
            String apkPath = "/data/app/com.app.exemplo/base.apk";
            String sdPath = Environment.getExternalStorageDirectory().getPath();
            try {
                File apk = new File(Environment.getDataDirectory()+apkPath);
                File directory = new File(sdPath + "/copiarAqui/");
                if (!directory.exists()){
                    directory.mkdirs();
                }
                input = new FileInputStream(Environment.getDataDirectory()
                        + apkPath);
                OutputStream output = new FileOutputStream(directory.getPath() +
                        "apktest.apk");
                byte[] buffer = new byte[1024];
                int lenght;
                while ((lenght = input.read(buffer)) > 0) {
                    output.write(buffer, 0, lenght);
                }

                output.flush();
                output.close();
                input.close();
                Toast.makeText(getActivity(), "Copiado com sucesso!", Toast.LENGTH_SHORT).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

Permissions are already in my application:

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

Can someone tell me the problem?

  • Are you testing on an emulator or real device? The contents of the folder /date/ is only available in an emulator or "Rooted device".

  • @ramaral Hello! I am testing on a real device. I thought it was not only for Rooted device, as it is also possible to backup installed apps without root. So I thought the /data/app/ folder would be able to read without root. Is there any way to backup by package name? Only from a package name, without listing all apps.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.