简体   繁体   中英

How to move an apk file from root/data/app/ to storage/emulated/0 programmatically in Android Studio

These files will display a list of installed applications on your mobile and when an app is clicked it will create(or apk already exists) apk for that app in /data/app/.But I want to move that file to /storage/emulated/0 I have tried using Environment,it is showing the same error as the former!

** In MainActivity.java**

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        ApplicationInfo app = applist.get(position);
        File apkFile = new File(app.publicSourceDir);
        String dest=Environment.getExternalStorageDirectory()+File.separator+"CodeViewer";
        String file=apkFile.getName();
        String src=Environment.getDataDirectory()+File.separator+"app"+File.separator+file;
        Toast.makeText(MainActivity.this,src+"   -  "+dest,Toast.LENGTH_LONG).show();
        moveFile(src,file,dest);
    }

    private void moveFile(String inputPath, String inputFile, String outputPath) {

        InputStream in = null;
        OutputStream out = null;
        try {

            File dir = new File (outputPath); 
            if (!dir.exists())
            {
                dir.mkdirs();
            }


            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;


            out.flush();
            out.close();
            out = null;


//          new File(inputPath + inputFile).delete();  


        } 

        catch (FileNotFoundException fnfe1) {
            Log.e("tag", fnfe1.getMessage());
            Toast.makeText(MainActivity.this,"failed",Toast.LENGTH_LONG).show();
        }
        catch (Exception e) {
            Log.e("tag", e.getMessage());
            Toast.makeText(MainActivity.this,"failed",Toast.LENGTH_LONG).show();
        }

    }

Is it possible?

Try

Runtime.getRuntime().exec("cp " + source + " " + destination);

To check if you have permissions to copy that file as non-root user, open terminal or adb shell and type that command manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM