open local pdf file Android

走远了吗. 提交于 2021-02-08 10:38:23

问题


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
}

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

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

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.

thanks, russ


回答1:


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....

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.");
 }


来源:https://stackoverflow.com/questions/58827928/open-local-pdf-file-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!