0
顯示PDF試圖顯示從服務器接收到一個PDF,但得到「沒有發現媒體的錯誤」Android的 - 從服務器
首先,我將字符串轉換爲字節數組:
byte[] data = Base64.decode(stringData, Base64.DEFAULT);
然後我寫的字節[ ]到一個文件:
String path = getFilesDir() + "/myfile.pdf";
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(file);
stream.write(article.getFileDataBytes());
stream.close();
然後我試圖顯示PDF:
Uri path2 = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path2, "application/pdf");
startActivity(intent);
但調用startActivity後,我收到了「未發現介質錯誤」
我不知道什麼是錯寫文件
不再需要清理文件的最佳做法是什麼? – MobileMon
@MobileMon:好的,問題是你不知道其他應用何時必須完成。我將這個文件放在'getCacheDir()'(我的示例使用'getFilesDir()')作爲初學者,所以如果磁盤空間不足,很容易擺脫。然後,我會啓動應用程序啓動過程中的一個線程,該線程擺脫了超過24小時的緩存文件。如果你有很多文件,或者真的很大的文件,或者類似的東西,你可能需要更積極。 「最佳做法」在這裏很難界定。 – CommonsWare