以编程方式将/system/app/*.apk复制到SD卡

时间:2013-10-11 06:32:23

标签: android copy apk

我有一个apk“/system/app/Gallery2.apk”的路径,我想在sdcard上复制它。我实现了复制方法

 public void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

但它显示IOException

我传递值

 try {
                    File file =new File( pm.getApplicationInfo(TAG_PACKAGE.get(position),PackageManager.GET_META_DATA).publicSourceDir);

                    Toast.makeText(MainActivity.this , pm.getApplicationInfo(TAG_PACKAGE.get(position),PackageManager.GET_META_DATA).publicSourceDir, Toast.LENGTH_LONG).show();

                try {
                    File dir = new File(Environment.getExternalStorageDirectory() + "/foldername/");
                     if(!dir.exists())
                        {
                            if(dir.mkdir()) ;//directory is created;
                            Toast.makeText(MainActivity.this ,dir.toString(), Toast.LENGTH_LONG).show();

                        }

                     copy(file.getAbsoluteFile(), dir);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                } catch (NameNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

例外:

  exception java.io.FileNotFoundException: /storage/sdcard0/folder: open failed: EISDIR (Is a directory)

它没有用,thnks

1 个答案:

答案 0 :(得分:1)

看起来您正在尝试将文件复制到文件夹,但未指定目标文件名。

我想你想把文件名附加到目标路径:

copy(file.getAbsoluteFile(), new File(dir, file.getName()));