stackoverflow
stackoverflow

Reputation: 2370

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

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

Upvotes: 2

Views: 1885

Answers (1)

Dalmas
Dalmas

Reputation: 26547

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

Upvotes: 1

Related Questions