繁体   English   中英

如何在 Android Studio 中以编程方式将 apk 文件从 root/data/app/ 移动到 storage/emulated/0

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

这些文件将显示您手机上已安装应用程序的列表,当单击应用程序时,它将在 /data/app/ 中为该应用程序创建(或 apk 已存在)apk。但我想将该文件移动到 /storage/emulated /0 我尝试使用环境,它显示与前者相同的错误!

** 在 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();
        }

    }

可能吗?

尝试

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

要检查您是否有权以非 root 用户身份复制该文件,请打开终端或 adb shell 并手动键入该命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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