如何在安卓工作室中以编程方式将apk文件从根/数据/应用程序/移动到存储/模拟/0



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

** 在主活动中.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 并手动键入该命令。

最新更新