繁体   English   中英

Android PDF加载时间

[英]Android PDF Load Timing

  • 要求:确保在用户离开PDF查看屏幕后从设备中删除PDF文档。

    • 问题:在某些设备(肯定是三星4.4.2和三星4.1.2,但不是华硕4.2.1)上,仅在重新启动应用程序后首次请求PDF ,才会显示错误消息,指出“此文档无法打开”。 此后,PDF将正常加载。 我认为这是一个时序问题,因为进程需要首次启动,但是在首次尝试加载后仍在运行。
    • 代码:请注意,首先调用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(); } } 

编辑:我也曾尝试实现一个回调,以绝对确定createFile()已完成其工作。 我什至尝试为Intent.FLAG_GRANT_READ_URI_PERMISSIONIntent.FLAG_GRANT_WRITE_URI_PERMISSIONIntent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION添加了(不同时间增量的)延迟(以及完全不必要的标记)。

我仍然不知道为什么会这样,但是这是万一其他人遇到此问题的解决方案:这是创建文件的目录。 由于某些原因,在两台三星设备上,文件的访问或创建方式与华硕设备不同。 因此, File path = new File(getExternalFilesDir(null).getAbsolutePath(), "temp"); 成为File path = new File(getExternalCacheDir().getAbsolutePath()); 问题就解决了。

暂无
暂无

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

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