Copy /system/app/*.apk to sdcard programmatically

筅森魡賤 提交于 2019-12-12 10:12:30

问题


i have path of one apk "/system/app/Gallery2.apk" and i want to copy this on sdcard. i implement copy method

 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();
}

but it shows IOException

i pass values

 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 :

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

it is not working , thnks


回答1:


It looks like you're trying to copy a file to a folder, but without specifying the destination file name.

I guess you want to append the file name to the destination path :

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


来源:https://stackoverflow.com/questions/19311690/copy-system-app-apk-to-sdcard-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!