android.os.FileUriExposedException:< filename>通过Intent.getData()在应用程序之外公开 [英] android.os.FileUriExposedException: <filename> exposed beyond app through Intent.getData()
问题描述
在早期版本的Delphi(Rio 10.3之前的版本)中,其目标是Android API<如图24所示,可以创建文件意图,如问题使用delphi使用外部库打开图像
但是,现在10.3的目标是Android API> = 24,则该代码会产生作为此问题主题的错误。
我竭力在 Delphi中回答问题,请使用android fileprovider发送打开和打开意图带有默认Android画廊的图像文件,但该问题已作为重复项关闭,即使更紧密链接的答案是在Android Java中,而不是在Delphi中。我的答案在下面(经过长时间研究之后)
使用
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Media,
Androidapi.Helpers,
Androidapi.JNI.Net,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.App,
Androidapi.JNI.Os,
Androidapi.JNI.Support,
System.IOUtils;
过程TTabbedForm.Button1Click(Sender:TObject);
var
Intent:JIntent;
FileName,DestFileName:字符串;
数据:Jnet_Uri;
CompName:JComponentName;
lFile:JFile;
const
IMAGE_FILENAME =‘small_what.jpg’;
开始
FileName:= System.IOUtils.TPath.GetPublicPath + PathDelim + IMAGE_FILENAME; //部署到资产
DestFileName:= TPath.GetDownloadsPath + PathDelim + IMAGE_FILENAME;
TFile.Copy(FileName,DestFileName,true);
Intent:= TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
如果TJBuild_VERSION.JavaClass.SDK_INT> = TJBuild_VERSION_CODES.JavaClass.N,则
开始
lFile:= TJFile.JavaClass.init(StringToJString(FileName));
Intent.setFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
数据:= TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context,
StringToJString('com.embarcadero.TestIntents.fileprovider'),lFile);
结束
否则
数据:= TJnet_Uri.JavaClass.parse(StringToJString(’file://’+ DestFileName));
Intent.setDataAndType(Data,StringToJString(’image / jpg’));
试试
TAndroidHelper.Activity.startActivity(Intent);
,但E上的
除外:
开始
Label1.Text:= E.Message;
结尾;
结尾;
In earlier versions of Delphi (prior to 10.3 Rio), which targeted Android API < 24, it was possible to create file intents as shown in the answer to question opening the image with the external gallery using delphi
However, now that 10.3 targets Android API >= 24, that code, produces the error that is the subject of this question.
I endeavoured to answer the question at Delphi Use android fileprovider to send intent to open and image file with the default android gallery but that question was closed as a duplicate even though the answer the closer linked to, is in Android Java and not Delphi. My answer is below (which follows after a few protracted hours of research)
uses
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Media,
Androidapi.Helpers,
Androidapi.JNI.Net,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.App,
Androidapi.JNI.Os,
Androidapi.JNI.Support,
System.IOUtils;
procedure TTabbedForm.Button1Click(Sender: TObject);
var
Intent: JIntent;
FileName, DestFileName: string;
Data: Jnet_Uri;
CompName: JComponentName;
lFile: JFile;
const
IMAGE_FILENAME = 'small_what.jpg';
begin
FileName := System.IOUtils.TPath.GetPublicPath + PathDelim + IMAGE_FILENAME; // deployed to "assets"
DestFileName := TPath.GetDownloadsPath + PathDelim + IMAGE_FILENAME;
TFile.Copy(FileName, DestFileName, true);
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
if TJBuild_VERSION.JavaClass.SDK_INT >= TJBuild_VERSION_CODES.JavaClass.N then
begin
lFile := TJFile.JavaClass.init(StringToJString(FileName));
Intent.setFlags(TJIntent.JavaClass.FLAG_GRANT_READ_URI_PERMISSION);
Data := TJFileProvider.JavaClass.getUriForFile(TAndroidHelper.Context,
StringToJString('com.embarcadero.TestIntents.fileprovider'), lFile);
end
else
Data := TJnet_Uri.JavaClass.parse(StringToJString('file://' + DestFileName));
Intent.setDataAndType(Data, StringToJString('image/jpg'));
try
TAndroidHelper.Activity.startActivity(Intent);
except
on E: Exception do
begin
Label1.Text := E.Message;
end;
end;
这篇关于android.os.FileUriExposedException:< filename>通过Intent.getData()在应用程序之外公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!