2

我有一个 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)

它不工作,谢谢

4

1 回答 1

1

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

我猜您想将文件名附加到目标路径:

copy(file.getAbsoluteFile(), new File(dir, file.getName()));
于 2013-10-17T09:34:52.863 回答