Java Android PDF加载定时

Java Android PDF加载定时,java,android,pdf,Java,Android,Pdf,要求:确保在用户离开PDF查看屏幕后从设备中删除PDF文档 问题:在某些设备上,三星4.4.2和三星4.1.2是肯定的,但不是Asus 4.2.1,只有在重新启动应用程序后第一次请求PDF时,才会显示错误消息,说明无法打开此文档。此后,PDF将正常加载。我认为这是一个时间问题,因为进程需要在第一次启动时启动,但在第一次尝试加载后运行。 代码:注意,首先调用createFile,然后调用startActivityForIntentResult 编辑:我还尝试实现回调,以绝对确定createFile

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

问题:在某些设备上,三星4.4.2和三星4.1.2是肯定的,但不是Asus 4.2.1,只有在重新启动应用程序后第一次请求PDF时,才会显示错误消息,说明无法打开此文档。此后,PDF将正常加载。我认为这是一个时间问题,因为进程需要在第一次启动时启动,但在第一次尝试加载后运行。 代码:注意,首先调用createFile,然后调用startActivityForIntentResult

编辑:我还尝试实现回调,以绝对确定createFile已经完成了它的工作。我甚至尝试过为Intent.FLAG\u GRANT\u READ\u URI\u PERMISSION、Intent.FLAG\u GRANT\u WRITE\u URI\u PERMISSION和Intent.FLAG\u GRANT\u PERSISTABLE\u URI\u PERMISSION添加不同时间增量的延迟,以及添加完全不必要的标志。

我仍然不知道为什么这样做,但如果其他人遇到这个问题,这里有一个解决方案: 它是创建文件的目录。由于某种原因,在两款三星设备上,文件的访问或创建方式与华硕设备有所不同。因此File path=new FilegetExternalFilesDirnull.getAbsolutePath,temp;变为文件路径=新文件GetExternalCacheDir.getAbsolutePath;问题就这样解决了

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();
}
}