在android中存储pdf文件的最佳方法
在我们的应用程序中,如果登录成功,我们将从服务器获取字节数组。我们正在将这些字节数组转换为PDF格式,并使用内存将这些文件存储到数据库中。如果文件以KB为单位,应用程序工作正常,但文件大小以MB为单位增加,然后应用程序出现内存不足错误。请告诉我如何处理此情况?如何将文件存储到SD卡以保持安全性。外部用户不应看到此情况。 请帮忙 谢谢,在android中存储pdf文件的最佳方法,android,Android,在我们的应用程序中,如果登录成功,我们将从服务器获取字节数组。我们正在将这些字节数组转换为PDF格式,并使用内存将这些文件存储到数据库中。如果文件以KB为单位,应用程序工作正常,但文件大小以MB为单位增加,然后应用程序出现内存不足错误。请告诉我如何处理此情况?如何将文件存储到SD卡以保持安全性。外部用户不应看到此情况。 请帮忙 谢谢, AA.存储在SD卡上可以让精明的用户访问该文件。存储在数据库中会出现您提到的错误。最好的办法可能是使用内部存储。这并不完美(根用户可以浏览文件),但可能是最好的选
AA.存储在SD卡上可以让精明的用户访问该文件。存储在数据库中会出现您提到的错误。最好的办法可能是使用内部存储。这并不完美(根用户可以浏览文件),但可能是最好的选择 您应该看看: 和。它们用于加密和解密字节流 编辑:那你来了 我有一个名为
cleartext
的文件。该文件包含:
现在,您有了一个encrypt()
函数:
static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
// Here you read the cleartext.
FileInputStream fis = new FileInputStream("data/cleartext");
// This stream write the encrypted text. This stream will be wrapped by another stream.
FileOutputStream fos = new FileOutputStream("data/encrypted");
// 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[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
// Flush and close streams.
cos.flush();
cos.close();
fis.close();
}
static void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream("data/encrypted");
FileOutputStream fos = new FileOutputStream("data/decrypted");
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8];
while((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
}
执行此函数后,应该有一个文件名加密的
。该文件包含加密字符
对于解密,您有decrypt
功能:
static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
// Here you read the cleartext.
FileInputStream fis = new FileInputStream("data/cleartext");
// This stream write the encrypted text. This stream will be wrapped by another stream.
FileOutputStream fos = new FileOutputStream("data/encrypted");
// 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[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
// Flush and close streams.
cos.flush();
cos.close();
fis.close();
}
static void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream("data/encrypted");
FileOutputStream fos = new FileOutputStream("data/decrypted");
SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8];
while((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
}
执行decrypt后,应该有一个名为decrypted
的文件。此文件包含自由文本
编辑:你写你是一个“noob”,但根据加密的使用情况,如果你没有正确的方法,你可能会造成很大的伤害。了解你的工具
CipherOutputStream的用法:
SecretKeySpec skeySpec=新的SecretKeySpec(y.getBytes(),“AES”)
因此,加密应该可以工作。当您反转该过程时,您应该能够读取解密的字节。DB也是指内部存储。您能告诉我如何在内部存储中存储文件吗。它还将给我内存不足错误。如果您将整个字节流下载到内存中,然后将其保存到数据库中,则很可能会出现内存不足错误。如果您直接从web请求的输入流读写到文件的输出流,那么您就不会在内存中保存所有这些字节,也不会占用所有分配的空间。