复制APK:为什么它不起作用?

时间:2013-10-29 15:02:02

标签: java android

我有问题。我有一个ListView和一个上下文菜单,上面写着“复制”。我希望当我点击“复制”时,将APK转移到存储/模拟/ 0 / 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;

}
}

当我点击复制时,我得到了这个吐司: /data/appcom.NameOfPackage.Package:open failed:ENOENT(没有这样的文件或目录)在指定的目录中。

我该如何解决?我想当我点击复制时拿APK应用程序并将其从/ data / app移动到/ storage / emulated / 0 / APK

1 个答案:

答案 0 :(得分:0)

使用Environment.getDataDirectory()代替data/data/....并使用Environment.getExternalStorageDirectory()代替storage/emulated/0...

希望有所帮助