我正在将pdf文件下载到“下载”文件夹中以进行查看。对于iOS和Win32,这很容易,因为TWebBrowser
可以很好地处理pdf文件。我的问题是与Android。下面是我的代码:
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查看器,但不会打开文件。
我猜我没有正确传递pdf文件名。有什么想法吗?
编辑:好的,file://
的使用已被弃用,似乎唯一的方法是通过FileProvider-每个this SE question。对于我这个水平的人来说,艰辛的路要走。我只想显示一个pdf。
谢谢, 鲁斯
答案 0 :(得分:0)
我知道了!戴夫(Dave)的帮助with this德尔福问题使我无动于衷。这是我的代码:
_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);
请注意,我必须确保在Project-> Options-> Application-> Entitlement List下的Secure File Sharing
是正确的,并且我必须确保将我的应用had access添加到设备存储中。很开心的一天。...
此外,这是相同的代码,但在末尾加了勾以确保有一个可以实际显示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.");
}