我遇到問題了。我有一個ListView和一個上下文菜單,上面寫着「Copy」。我希望當我點擊「複製」時採取APK並從數據/應用程序移動到存儲/模擬/ 0/APK。我有這個代碼。複製APK:爲什麼它不起作用?
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
final long examId = info.id;
ApplicationInfo app = applist.get((int) info.id);
switch (item.getItemId()) {
case COPY:
{
try{
File f1 = new File("/data/app"+app.packageName);
File f2 = new File("storage/emulated/0/APK");
InputStream in = new FileInputStream(f1);
//For Append the file.
// OutputStream out = new FileOutputStream(f2,true);
//For Overwrite the file.
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
Toast.makeText(getBaseContext(), "Success", Toast.LENGTH_SHORT).show();
}
catch(FileNotFoundException ex){
Toast.makeText(getBaseContext(), ex.getMessage() + " in the specified directory.", Toast.LENGTH_SHORT).show();
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
return true;
}
}
我得到這個麪包當我點擊複製:在指定的目錄ENOENT(沒有這樣的文件或目錄): /data/appcom.NameOfPackage.Package:打開失敗。
我該如何解決?我想,當我在複印單擊採取APK應用程序,它從/數據/應用程序移動到/存儲/模擬/ 0/APK
不應該指定**「/ data/app /」**(最後的額外斜線)而不是「/ data/app」? –
是的,斜線缺失。但是,您試圖做的事情可能不允許在最近或未來的Android版本中使用。 –
爲什麼不正確?所以要將APK從一個文件夾移動到另一個文件夾,我應該怎麼做?我很困惑。 –