Android 从AssetManager以InputStream形式读取文件后发生java.lang.NullPointerException
AssetManager的定义和使用:Android 从AssetManager以InputStream形式读取文件后发生java.lang.NullPointerException,android,Android,AssetManager的定义和使用: ... AssetManager mngr = getAssets(); try{ encrypter.decrypt(mngr.open("sample.txt"),output_file); }...(continued) 解密函数: public void decrypt(InputStream in, OutputStream out) { try {
...
AssetManager mngr = getAssets();
try{
encrypter.decrypt(mngr.open("sample.txt"),output_file);
}...(continued)
解密函数:
public void decrypt(InputStream in, OutputStream out)
{
try
{
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
while ((numRead = in.read(buf)) >= 0)
{
out.write(buf, 0, numRead);
}
out.close();
...(continued)
它给行out.write(buf,0,numRead)的错误作为java.lang.NullPointerException
此呼叫在用作以下用途时工作正常:
*encrypter.decrypt(新文件输入流(“sample.txt”),输出文件)*(即从android的本地资产目录读取文件时)
有什么原因吗
感谢您的帮助!谢谢 您的OutputStream未初始化。因此您将获得空指针异常。您可能需要 像这样的
public void decrypt(InputStream in,String file)
{
try
{
OutputStream out = new FileOutputStream(file);
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
while ((numRead = in.read(buf)) >= 0)
{
out.write(buf, 0, numRead);
}
out.close();
您的OutputStream未初始化。因此您得到的是空指针异常。您可能需要 像这样的
public void decrypt(InputStream in,String file)
{
try
{
OutputStream out = new FileOutputStream(file);
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);
// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
while ((numRead = in.read(buf)) >= 0)
{
out.write(buf, 0, numRead);
}
out.close();
显示完整代码,我的意思是声明哪部分的声明。。AssetManager?显示完整代码,我指的是声明哪部分的声明。。AssetManager?谢谢问题正是你说的。。非常感谢,谢谢问题正是你说的。。非常感谢。