代码之家  ›  专栏  ›  技术社区  ›  Lavin_38

对多个文件执行函数

  •  0
  • Lavin_38  · 技术社区  · 6 年前

    我正在尝试在Android上用下面的代码加密文件夹中的多个视频文件。但是我当前的代码没有按预期工作。应用程序加密一个文件并停止。基本上,代码应该能够浏览给定文件夹中的文件列表,并对每个文件进行加密。希望能得到以下方面的帮助。

    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<String, String, String> {
            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();
    
        }
    }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Lan Nguyen    6 年前

    显然,您只能在一个线程(encrypttask)上按顺序加密视频。 对于每个文件,需要一个离散的线程进行加密。像这样更改代码:

    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) {
                encrypt();
            }
        });
    }
    
    
    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())
            {
                new EncryptTask(file).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            }
        }
        public class EncryptTask extends AsyncTask<String, String, String> {
    
            @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
    
        @Override
        protected String doInBackground(String... params) {
            //Do encrypt file
            return null;
        }
    
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);    
        }
    }
    }