简体   繁体   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

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

** In MainActivity.java** ** 在 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.要检查您是否有权以非 root 用户身份复制该文件,请打开终端或 adb shell 并手动键入该命令。

暂无
暂无

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

相关问题 如何将非媒体文件从 /storage/emulated/0/Download 移动到 getExternalFilesDir() - How to move a non media file from /storage/emulated/0/Download to getExternalFilesDir() Android Studio,/ storage / emulated / 0不存在 - Android studio, /storage/emulated/0 not exist Android Studio-从Windows资源管理器和Android文件管理器访问/ storage / emulated / 0 - Android Studio - Acces to /storage/emulated/0 from windows explorer and android file manager 如何以编程方式从sdcard / storage .apk文件获取appicon? - How to get appicon programmatically from sdcard/storage .apk file? FileSystemException:无法打开文件,路径 = '目录:'/storage/emulated/0/Android/data/ - FileSystemException: Cannot open file, path = 'Directory: '/storage/emulated/0/Android/data/ java.io.FileNotFoundException:/storage/emulated/0/Android/data/MyApplication/MyFile.ics。 (没有这样的文件或目录)/ Android Studio / Ical4j - java.io.FileNotFoundException: /storage/emulated/0/Android/data/MyApplication/MyFile.ics. (No such file or directory) / Android Studio / Ical4j android 11中/storage/emulated/路径下的read.txt字段数据 - Read .txt field data from /storage/emulated/ path in android 11 如何从 android 的内部存储器中移动文件 a? - How to move file a from internal storage of android? 如何在Android上将文件从内部应用程序存储移动/重命名为外部存储? - How to move/rename file from internal app storage to external storage on Android? 如何停止模拟应用程序的运行? (Android Studio,64位Win7 SP1) - How to stop emulated app from running? (Android Studio, 64-bit Win7 SP1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM