以下のコードを使用してファイルをリソースフォルダーにリンクし、ファイルの読み取りと書き込みを行ってから、サードパーティのアプリを介して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".
しかし、assest managerを使用してファイルを開くと、完全に正常に機能します。
ここで何が問題なのか、