Trong onCreate của tôi() Tôi làm việc kiểm tra này:Hiện PDF trong Android
//
// check if we have a PDF viewer, else bad things happen
//
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("application/pdf");
List<ResolveInfo> intents = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (intents == null || intents.size() == 0) {
// display message then...
finish();
}
On HTC Desire của tôi, điều này không trả về một trận đấu, mặc dù tôi có xem Adobe PDF. Câu trả lời cho câu hỏi này android: open a pdf from my app using the built in pdf viewer đề cập rằng Adobe có thể không có bất kỳ Mục đích công khai nào, vì vậy việc kiểm tra ở trên rõ ràng sẽ không trả lại gì.
Bất kỳ ai cũng có thể xác minh xem bạn có thể khởi chạy Acrobat từ mục đích hay không hoặc có phương pháp hoặc trình xem PDF nào khác để sử dụng.
Các trường hợp sử dụng thực tế là tải bản sao hoá đơn và lưu trữ chúng trên bộ nhớ cục sử dụng mã như:
URL url = new URL(data);
InputStream myInput = url.openConnection().getInputStream();
FileOutputStream fos = openFileOutput(fname, Context.MODE_WORLD_READABLE);
// transfer bytes from the input file to the output file
byte[] buffer = new byte[8192];
int length;
while ((length = myInput.read(buffer)) > 0) {
fos.write(buffer, 0, length);
progressDialog.setProgress(i++);
}
fos.close();
và sau đó để hiển thị
// read from disk, and call intent
openFileInput(fname); // will throw FileNotFoundException
File dir = getFilesDir(); // where files are stored
File file = new File(dir, fname); // new file with our name
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file));
intent.setType("application/pdf");
startActivity(intent);
Cảm ơn - ý tưởng hay – BJB