title: Performing a function on multiple files
body: <p>I'm trying to encrypt multiple video files in a folder with the below code on Android. But my current code is not working as desired. The app encrypts one file and stops. Essentially the code should be able to go through a list of files in the given folder and encrypt each and every one of them. Would appreciate any help I can receive on the below.</p>
  
  <pre><code>package in.org.connected.hercules;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.security.InvalidKeyException;
  import java.security.NoSuchAlgorithmException;
  
  import javax.crypto.Cipher;
  import javax.crypto.CipherOutputStream;
  import javax.crypto.NoSuchPaddingException;
  import javax.crypto.spec.SecretKeySpec;
  
  public class MainActivity extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      Button encryptbutton = (Button) findViewById(R.id.button1);
  
      encryptbutton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              new EncryptTask().execute();
  
          }
      });
  }
  
  
  static void encrypt() throws IOException, NoSuchAlgorithmException,
          NoSuchPaddingException, InvalidKeyException {
      // Here you read the cleartext.
      File dir = new File("/mnt/usbhost0");
      if (dir.isDirectory()){
          for(File file : dir.listFiles())
          {
              FileInputStream fis = new FileInputStream(file.getAbsolutePath());
              String fileName = file.getName();// This stream write the encrypted text. This stream will be wrapped by
              // another stream.
              FileOutputStream fos = new FileOutputStream("/mnt/usbhost0/(enc)" + fileName);
  
              // Length is 16 byte
              SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
                      "AES");
              // Create cipher
              Cipher cipher = Cipher.getInstance("AES");
              cipher.init(Cipher.ENCRYPT_MODE, sks);
              // Wrap the output stream
              CipherOutputStream cos = new CipherOutputStream(fos, cipher);
              // Write bytes
              int b;
              byte[] d = new byte[16384];
              while ((b = fis.read(d)) != -1) {
                  cos.write(d, 0, b);
              }
              // Flush and close streams.
              cos.flush();
              cos.close();
              fis.close();
              }
          }
      }
      public class EncryptTask extends AsyncTask&lt;String, String, String&gt; {
          ProgressDialog pd;
  
          @Override
      protected void onPreExecute() {
          super.onPreExecute();
          pd = new ProgressDialog(MainActivity.this);
          pd.setMessage("Encrypting your video");
          pd.show();
  
      }
  
  
      @Override
      protected String doInBackground(String... params) {
          try {
              encrypt();
          } catch (InvalidKeyException e) {
              e.printStackTrace();
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace();
          } catch (NoSuchPaddingException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return null;
      }
  
      @Override
      protected void onPostExecute(String s) {
          super.onPostExecute(s);
          pd.dismiss();
  
      }
  }
  }
  </code></pre>