Проблема с файлом ввода потока Java android

Мне нужен небольшой вопрос. Как я могу восстановить этот код, чтобы я мог использовать его и в Android. Мне нужно просто загрузить файл из папки активов в Android Projet, расшифровать его и показать размер файла и сколько времени это займет к приложению, чтобы расшифровать его.

Код :

package decryption;

import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;

    public class Decryption {
      public static void main(String args[]) throws Exception {


          File file = new File("ecryption.pdf");
          System.out.println(file.getAbsolutePath());
          System.out.println("user.dir is: " + System.getProperty("user.dir"));

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

        FileInputStream fis   = new FileInputStream(new File("ecrypted.pdf"));
        long start = System.currentTimeMillis();
        System.out.print(start+"           ");
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        FileOutputStream fos  = new FileOutputStream(new File("decrypted.pdf"));
        long end = System.currentTimeMillis();
        System.out.print(end);


        byte[] b = new byte[8];
        int i;

        while ((i = cis.read(b)) != -1) {
          fos.write(b, 0, i);
        }
        fos.flush(); fos.close();
        cis.close(); fis.close();       
      }
}

person Android-Droid    schedule 29.07.2011    source источник


Ответы (2)


//      File file = new File("ecryption.pdf");
//      System.out.println(file.getAbsolutePath());
//      System.out.println("user.dir is: " + System.getProperty("user.dir"));

// FileInputStream fis   = new FileInputStream(new File("ecrypted.pdf"));
InputStream fis = getAssets().open("ecryption.pdf");

// FileOutputStream fos  = new FileOutputStream(new File("decrypted.pdf"));
FileOutputStream fos  = new FileOutputStream(
       new File(Environment.getExternalStorageDirectory(), "decrypted.pdf"));

Затем нужно скомпилировать и настроить остальные.

person Vincent Mimoun-Prat    schedule 29.07.2011
comment
Этот код вызывает у меня исключение: 07-29 13:02:29.693: VERBOSE/Error(1112): Error java.io.FileNotFoundException: /sdcard/decrypted.pdf - person Android-Droid; 29.07.2011
comment
Затем исправить и отладить программу. Не ждите, что кто-то сделает все за вас. Я поставил тебя на путь. - person Vincent Mimoun-Prat; 29.07.2011
comment
Большое спасибо, я исправил ошибку. Я забыл добавить разрешение в файл манифеста. - person Android-Droid; 29.07.2011

это будет копировать один файл за раз ... начать оттуда ..

    public void copyAssets() {

    try {
        in = getAssets().open("aabbccdd.mp3");
        File outFolder = new File(root.getAbsolutePath() + "/testfolder182");
        outFolder.mkdir();
        File outFile = new File(outFolder, "ooooooooohhhigetit.mp3");
        out = new FileOutputStream(outFile);
        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (IOException e) {
        Log.e("tag", "Failed to copy asset file: ", e);
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}
person user3315096    schedule 16.02.2014