unit Android.FileProviderService;
interface
uses
System.SysUtils
, System.ioutils
, Androidapi.JNI.Os
, Androidapi.JNI.GraphicsContentViewText
, Androidapi.JNI.JavaTypes
, Androidapi.Helpers
, Androidapi.JNI.Net
, Androidapi.JNI.Webkit
;
type
TFileProviderService =
Class
public
Class procedure SendFileViaFileProvideToChooser(file_name:
string);
Class procedure ViewFileViaFileProvider(file_name:
string);
End;
implementation
Class procedure TFileProviderService.SendFileViaFileProvideToChooser(file_name:
string);
var
ExtFile,id, content:
string;
mime: JMimeTypeMap;
ExtToMime: JString;
Intent: JIntent;
javafile: JFile;
FileURI:JNet_URI;
ParcelURI:JParcelable;
begin
javafile:=TJFile.JavaClass.init(StringToJString(file_name));
ExtFile := AnsiLowerCase(StringReplace(TPath.GetExtension(file_name), '
.', '
',[]));
mime := TJMimeTypeMap.JavaClass.getSingleton();
ExtToMime := mime.getMimeTypeFromExtension(StringToJString(ExtFile));
Intent := TJIntent.Create;
id := JStringToString( TAndroidHelper.Context.getApplicationContext.getPackageName ) + '
.fileprovider';
content := '
content://' + id + '
/root' + Jstringtostring( javafile.getCanonicalPath );
FileURI := TJnet_Uri.JavaClass.parse(StringtoJString(content));
Intent.setAction(TJIntent.JavaClass.ACTION_SEND);
Intent.SetType(ExtToMime);
ParcelURI := JParcelable(FileURI);
Intent.PutExtra(TJIntent.JavaClass.EXTRA_STREAM, ParcelURI);
SharedActivity.startActivity(TJIntent.JavaClass.createChooser(intent, StrToJCharSequence ('
Share With')));
end;
Class procedure TFileProviderService.ViewFileViaFileProvider(file_name:
string);
var
ExtFile,id, content:
string;
mime: JMimeTypeMap;
ExtToMime: JString;
Intent: JIntent;
javafile: JFile;
FileURI:JNet_URI;
begin
javafile:=TJFile.JavaClass.init(StringToJString(file_name));
ExtFile := AnsiLowerCase(StringReplace(TPath.GetExtension(file_name), '
.', '
',[]));
mime := TJMimeTypeMap.JavaClass.getSingleton();
ExtToMime := mime.getMimeTypeFromExtension(StringToJString(ExtFile));
Intent := TJIntent.Create;
id := JStringToString( TAndroidHelper.Context.getApplicationContext.getPackageName ) + '
.fileprovider';
content := '
content://' + id + '
/root' + Jstringtostring( javafile.getCanonicalPath );
FileURI := TJnet_Uri.JavaClass.parse(StringtoJString(content));
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setFlags(1);
Intent.setDataAndType(FileURI, ExtToMime);
// Intent.setDataAndType(StrToJURI(deger), ExtToMime);
SharedActivity.startActivity(Intent);
end;