使用API​​ 26的Delphi Android文件打开失败 [英] Delphi Android file open failure with API 26

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

问题描述

我可以使用下面显示的Delphi代码在Android上打开文件。但是,当我在API 26上编译它时,它给出了我在下图中添加的错误。我该如何解决这个问题?

I can open files on Android with the Delphi code shown below. But when I compile it at API 26, it gives the error I added in the picture below. How can I solve this problem?

ExtFile := AnsiLowerCase(StringReplace(TPath.GetExtension(yol), '.', '',[]));
mime := TJMimeTypeMap.JavaClass.getSingleton();
ExtToMime := mime.getMimeTypeFromExtension(StringToJString(ExtFile));
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setDataAndType(StrToJURI('file:' + yol), ExtToMime);
SharedActivity.startActivity(Intent);

非常感谢您的帮助。我很高兴终于在这个平台上遇到了这种了解人。我尝试了您发送的.pas文件。但我看到了另一个错误窗口。我分享我的代码和错误。

Thank you so much for your help. I'm glad that I finally came across this kind of understanding person on this platform. I tried the .pas file you sent. but I see a different error window. I share my codes and the error. thank you so much.

var
  ExtFile,yol,deger,id:string;
  mime: JMimeTypeMap;
  ExtToMime: JString;
  Intent: JIntent;
  javafile:JFile;
begin
  yol:='/sdcard/SkyWiFiDownload/sancak.jpg';
  javafile:=TJFile.JavaClass.init(StringToJString(yol));
  ExtFile := AnsiLowerCase(StringReplace(TPath.GetExtension(yol), '.', '',[]));
  mime := TJMimeTypeMap.JavaClass.getSingleton();
  ExtToMime := mime.getMimeTypeFromExtension(StringToJString(ExtFile));
  Intent := TJIntent.Create;
  id:=JStringToString(TAndroidHelper.Context.getApplicationContext.
  getPackageName) + '.fileprovider';
  deger:=JURIToStr(TJFileProvider.JavaClass.getUriForFile(
  TAndroidHelper.Context,StringToJString(id),javafile));
  Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
  Intent.setFlags(1);
  Intent.setDataAndType(StrToJURI(deger), ExtToMime);
  SharedActivity.startActivity(Intent);
end;  



(源: resmim.net

推荐答案

我终于想出了必要的步骤来完成这项工作。首先,您将需要修改AndroidManifest.Template.xml文件以添加Provider部分,如下所示:

I've finally come up with the necessary steps to make this work. Firstly, you will need to modify your AndroidManifest.Template.xml file to add a Provider section like this:

...
    <application android:persistent="%persistent%" 
        android:restoreAnyVersion="%restoreAnyVersion%" 
        android:label="%label%" 
        android:debuggable="%debuggable%" 
        android:largeHeap="%largeHeap%"
        android:icon="%icon%"
        android:theme="%theme%"
        android:hardwareAccelerated="%hardwareAccelerated%">
        <!-- **** ADD THIS SECTION **** -->
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.embarcadero.yourAppName.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
        </provider>

<%application-meta-data%>
        <%services%>
...

对于值:

  android:authorities="com.embarcadero.yourAppName.fileprovider"

将包名称更改为com.embarcadero.yourAppName。

Change: com.embarcadero.yourAppName to whatever your package name is.

您还需要创建一个文件:provider_paths.xml。我的看起来像这样:

You will also need to create a file: provider_paths.xml. Mine looks like this:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-path name="external_files" path="."/>
</paths>

..并使用Deployment Manager将其添加到您的项目部署中,其Remote Path值为: res\xml\

..and add it to your projects deployment using the Deployment Manager, with a Remote Path value of: res\xml\

从以下位置更改您的Delphi代码:

Change your Delphi code from:

Intent.setDataAndType(StrToJURI(deger), ExtToMime);

至:

Intent.setDataAndType(TAndroidHelperEx.UriFromFile(javafile), ExtToMime);

TAndroidHelperEx来自此单元:

TAndroidHelperEx comes from this unit:

https://github.com/DelphiWorlds/KastriFree/ blob / master / Core / DW.Android.Helpers.pas

它依赖于此单位:

https://github.com /DelphiWorlds/KastriFree/blob/master/API/DW.Androidapi.JNI.FileProvider.pas

这篇关于使用API​​ 26的Delphi Android文件打开失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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