我正在使用下面的代码将文件链接到资源文件夹并读取文件并写入,然后我通过 3rd 方应用程序打开 pdf 文件。
这是我的代码;
public void readFile(){
InputStream fileInputStream= null;
String filePath = "PDF/" + name + ".pdf";
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
fileInputStream = classLoader.getResourceAsStream(filePath);
int size = fileInputStream.available();
byte[] buffer = new byte[size];
fileInputStream.read(buffer);
fileInputStream.close();
writeFile(buffer, name);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void writeFile( byte[] buffer, String fileName){
try {
File root = Environment.getExternalStorageDirectory();
FileOutputStream fileOutputStream = null;
if (root.canWrite()){
File pdffile = new File("/sdcard/aaa/bbb");
pdffile.mkdirs();
System.out.println(" pdf path "+pdffile.toString());
File outputFile = new File(pdffile.toString());
fileOutputStream = new FileOutputStream(
outputFile+"/" + fileName + ".pdf");
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);
bos.write(buffer);
bos.flush();
bos.close();
}
} catch (IOException e) {
Log.e("Rrror", "Could not write file " + e.getMessage());
}
}
File f = new File("/sdcard/aaa/bbb/"+name+".pdf");
Uri path = Uri.fromFile(f);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
现在有些文件可以正确读取和打开,但很少有文件没有打开它说 K
"This Document cannot be opened".
但是,如果我使用资产管理器打开文件,它工作得很好。
这里有什么问题,