Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Save files from a sdcard folder to another place

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 222
    Comment on it

    Here below I have written code for copy file from sdcard and paste it to another place.

    I do this by using InputStream and OutputStream classes. Your target file should exist before doing this task.

        // your sdcard
        String sdCardString = Environment.getExternalStorageDirectory().toString();
    
       //
        File sourceFile = new File (sdCard + "/abc.txt");
    
        // make sure your target location folder exists!
        File targetFile = new File (sdCard + "/NewFolder/abc.txt");
    
        // just to take note of the location sources
        Log.v(TAG, "sourceFile: " + sourceFile);
        Log.v(TAG, "targetFile: " + targetFile);
    
        try {
    
            // 1 = move the file, 2 = copy the file
            int choose = 2;
    
            // moving the file to another directory
            if(choose==1){
    
                if(sourceFile.renameTo(targetFile)){
    
    System.out.println("successfully move file.")
    
             }else{
    
    
    

    System.out.println("Move file failed.")

    } } // we will copy the file else{ // make sure the target file exists if(sourceFile.exists()){ InputStream inStream = new FileInputStream(sourceFile); OutputStream outStream = new FileOutputStream(targetFile); // Copy the bits from instream to outstream byte[] buf = new byte[1024]; int length; while ((length = inStream.read(buf)) > 0) { outStream.write(buf, 0, length); } inStream.close(); outStream.close(); System.out.println("successfully copy file.") }else{ System.out.println("Copy file failed. Source file missing.") } } } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: