简体   繁体   English

Android PDF加载时间

[英]Android PDF Load Timing

  • The requirements: ensure that the PDF document is deleted from the device after the user has left the PDF viewing screen 要求:确保在用户离开PDF查看屏幕后从设备中删除PDF文档。

    • The problem: on certain devices (Samsung 4.4.2 and Samsung 4.1.2 for sure, but not Asus 4.2.1) only the first time that the PDF is requested after restarting the application an error message is displayed stating "This document cannot be opened". 问题:在某些设备(肯定是三星4.4.2和三星4.1.2,但不是华硕4.2.1)上,仅在重新启动应用程序后首次请求PDF ,才会显示错误消息,指出“此文档无法打开”。 Thereafter the PDF will load normally. 此后,PDF将正常加载。 I'm thinking this is a timing issue due to processes that need to be started the first time, but are running after the first attempted load. 我认为这是一个时序问题,因为进程需要首次启动,但是在首次尝试加载后仍在运行。
    • The code: note that createFile() is called first, then startActivityForIntentResult() 代码:请注意,首先调用createFile() ,然后调用startActivityForIntentResult()

       private File file; private ArrayList<Uri> uriList = new ArrayList<Uri>(); private void createFile() { int fileNameLength = pdfFileName[0].length(); String fileName = pdfFileName[0].substring(0, fileNameLength - 4) + DateTime.now(); String fileExtension = pdfFileName[0].substring(fileNameLength - 4, fileNameLength); byte[] content = Base64.decodeBase64(pdfData[0].getBytes()); BufferedOutputStream outputStream = null; try { File path = new File(getExternalFilesDir(null).getAbsolutePath(), "temp"); if (!path.exists()) { path.mkdirs(); } file = new File(path, fileName + fileExtension); outputStream = new BufferedOutputStream(new FileOutputStream(file)); outputStream.write(content); file.deleteOnExit(); uriList.add(Uri.fromFile(file)); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } finally { try { if (outputStream != null) { outputStream.flush(); outputStream.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } private static int REQUEST_CODE = 1; private Intent intent; private void startActivityForIntentResult() { if (file.exists()) { Uri targetUri = uriList.get(0); intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(targetUri, "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); try { startActivityForResult(intent, REQUEST_CODE); } catch (ActivityNotFoundException e) { toastTitle = "Error Displaying PDF"; toastMessage = "Please make sure you have an application for viewing PDFs installed on your device and try again."; toast = new GenericCustomToast(); toast.show(toastTitle, toastMessage, QueryForPDF.this); } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (resultCode == RESULT_CANCELED && requestCode == REQUEST_CODE) { if(!file.delete()) { file.delete(); } } searchAgain(); } @Override public void onBackPressed() { super.onBackPressed(); if(!file.delete()) { file.delete(); } searchAgain(); } @Override public void onStop() { super.onStop(); if(!file.delete()) { file.delete(); } } @Override public void onDestroy() { super.onDestroy(); if(!file.delete()) { file.delete(); } } 

EDIT: I have also tried implementing a callback to be absolutely certain that createFile() has finished it's work. 编辑:我也曾尝试实现一个回调,以绝对确定createFile()已完成其工作。 I even tried adding delays (of different time increments) as well as adding (the completely unnecessary) flags for Intent.FLAG_GRANT_READ_URI_PERMISSION , Intent.FLAG_GRANT_WRITE_URI_PERMISSION , and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION . 我什至尝试为Intent.FLAG_GRANT_READ_URI_PERMISSIONIntent.FLAG_GRANT_WRITE_URI_PERMISSIONIntent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION添加了(不同时间增量的)延迟(以及完全不必要的标记)。

I still don't know why this works, but here's the solution in case anyone else runs into this issue: It's the directory where the file is created. 我仍然不知道为什么会这样,但是这是万一其他人遇到此问题的解决方案:这是创建文件的目录。 For some reason on the two Samsung devices there was something different in how the files were either accessed or created versus the Asus device. 由于某些原因,在两台三星设备上,文件的访问或创建方式与华硕设备不同。 So File path = new File(getExternalFilesDir(null).getAbsolutePath(), "temp"); 因此, File path = new File(getExternalFilesDir(null).getAbsolutePath(), "temp"); becomes File path = new File(getExternalCacheDir().getAbsolutePath()); 成为File path = new File(getExternalCacheDir().getAbsolutePath()); and the problem goes away. 问题就解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM