打开本地pdf文件Android [英] open local pdf file Android

查看:92
本文介绍了打开本地pdf文件Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将pdf文件下载到下载"文件夹中以进行查看.对于iOS和Win32,这很容易,因为 TWebBrowser 可以很好地处理pdf文件.我的问题是与Android.下面是我的代码:

I'm downloading a pdf file to the Downloads folder to view. For iOS and Win32 this is easy because the TWebBrowser handles pdf files fine. My problem is with Android. Below is my code:

if (FileExists(LFileName))    // pdf file is there
{
 #if defined(_PLAT_IOS) || defined(_PLAT_MSWINDOWS)
  Form1->WebBrowser1->URL = "file://" + LFileName;
  Form1->WebBrowser1->Visible = true;
 #endif

 #if defined(_PLAT_ANDROID)
  Androidapi::Jni::Graphicscontentviewtext::_di_JIntent intent = 
  TJIntent::Create();
  intent->setDataAndType(StringToJString("file://" + System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetSharedDownloadsPath(), LFileName)), StringToJString(L"application/pdf"));
  intent->setAction(TJIntent::JavaClass->ACTION_VIEW);
  if (SharedActivity()->getPackageManager()->queryIntentActivities(intent, TJPackageManager::JavaClass->MATCH_DEFAULT_ONLY)->size() > 0) {
   SharedActivity()->startActivity(intent);
  } else {
   ShowMessage("PDF viewer not found");
  }
 #endif
}

当我在Android上运行它时,它会打开Adobe PDF查看器,但不会打开文件.

When i run it on an Android it opens Adobe PDF viewer but does not open the file.

我猜我没有正确传递pdf文件名.有什么想法吗?

I'm guessing that i'm not passing the pdf filename properly. Any ideas?

编辑:好的,不建议使用 file://,并且看来唯一的途径是通过FileProvider-每个

EDIT: Ok, use of file:// is deprecated and it looks like only way to go is via FileProvider - per this SE question. Miles of hard road for someone at my level. I just want to display a pdf.

谢谢,鲁斯

推荐答案

我知道了!戴夫的帮助与此 delphi问题有关我在驼峰上.这是我的代码:

I GOT IT! Dave's help with this delphi question got me over the hump. Here is my code:

 _di_JIntent MyIntent;
 _di_Jnet_Uri Uri;
 UnicodeString URLtest;
 URLtest = System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetSharedDownloadsPath(), "sample.pdf");
 Uri = TAndroidHelper::JFileToJURI(TJFile::JavaClass->init(StringToJString(URLtest)));
 MyIntent = TJIntent::JavaClass->init(TJIntent::JavaClass->ACTION_VIEW);
 MyIntent->setData(Uri);
 MyIntent->addFlags(TJIntent::JavaClass->FLAG_GRANT_READ_URI_PERMISSION);
 TAndroidHelper::Activity->startActivity(MyIntent);

请注意,我必须确保在项目"->选项"->应用程序"->权利列表"下,安全文件共享是真实的,并且我必须确保我的应用程序

Note, i had to make sure Secure File Sharing is true under Project->Options->Application->Entitlement List, and i had to make sure my app had access to the devices Storage. Very happy day....

此外,这是相同的代码,但在末尾加了勾以确保有一个可以实际显示pdf的应用程序:

Also, here is that same code but with a check at the end to make sure there is an app that can actually display a pdf:

_di_JIntent MyIntent;
_di_Jnet_Uri Uri;
 UnicodeString URLtest;
 URLtest = System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetSharedDownloadsPath(), "2009_FDA_paper.pdf");
 Uri = TAndroidHelper::JFileToJURI(TJFile::JavaClass->init(StringToJString(URLtest)));
 MyIntent = TJIntent::JavaClass->init(TJIntent::JavaClass->ACTION_VIEW);
 MyIntent->setData(Uri);
 MyIntent->addFlags(TJIntent::JavaClass->FLAG_GRANT_READ_URI_PERMISSION);
 //TAndroidHelper::Activity->startActivity(MyIntent);
 SharedActivity()->startActivity(MyIntent);
 if (SharedActivity()->getPackageManager()->queryIntentActivities(MyIntent, TJPackageManager::JavaClass->MATCH_DEFAULT_ONLY)->size() > 0) {
   SharedActivity()->startActivity(MyIntent);
 } else {
    ShowMessage("PDF viewer app not found.");
 }

这篇关于打开本地pdf文件Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆