-1

i want copy pdf file from application path(/data/data/package name) to sdcard.for that i have prepared following ,

try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    }
    finally {
        if(source != null) {
            source.close();
        }
        if(destination != null) {
            destination.close();
        }
    }

it is not working .PLease help.

SuReSh PaTi
  • 1,373
  • 7
  • 28
  • 46
  • The supported way of doing this is to put your file of interest in the assets folder of the package, and use the and use assetManager.open() to get an input stream as in http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard. The hack solution is to know that the apk is a form of zip file and open it with the zip file classes. – Chris Stratton Jun 29 '12 at 15:32

1 Answers1

1

Here is sample code for copying file

 private static void copyfile(String srFile, String dtFile){
        try{
            File f1 = new File(Source Fine Name);
            File f2 = new File(Destination File Name);
            InputStream in = new FileInputStream(f1);

//                  If you want to 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();
            System.out.println("File copied.");
        }
        catch(FileNotFoundException ex){
            System.out.println(ex.getMessage());

        }
        catch(IOException e){
            System.out.println(e.getMessage());         
        }
    }
rajpara
  • 5,203
  • 1
  • 29
  • 46