|
@@ -16,7 +16,7 @@ unit ksAndroid.Helpers;
|
|
|
interface
|
|
|
|
|
|
uses
|
|
|
- System.SysUtils,
|
|
|
+ System.SysUtils, Androidapi.JNIBridge,
|
|
|
Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.App,
|
|
|
Androidapi.JNI.JavaTypes, Androidapi.JNI.Net;
|
|
|
|
|
@@ -77,6 +77,22 @@ type
|
|
|
class function IsActivityForeground: Boolean; static;
|
|
|
|
|
|
/// <summary>
|
|
|
+ /// Get column data from uri
|
|
|
+ /// </summary>
|
|
|
+ class function GetColumnAsString(const AUri: Jnet_Uri; const AColumn: JString; ASelection: JString = nil;
|
|
|
+ ASelectionArgs: TJavaObjectArray<JString> = nil; ASortOrder: JString = nil): string; static;
|
|
|
+ /// <summary>
|
|
|
+ /// Get data column data from uri
|
|
|
+ /// </summary>
|
|
|
+ class function GetDataColumnAsString(const AUri: Jnet_Uri; ASelection: JString = nil;
|
|
|
+ ASelectionArgs: TJavaObjectArray<JString> = nil; ASortOrder: JString = nil): string; static;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Converts uri to file path
|
|
|
+ /// </summary>
|
|
|
+ class function FileFromUri(const AUri: Jnet_Uri): string; overload; static;
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
/// Converts file to uri, without FileProvider
|
|
|
/// </summary>
|
|
|
class function UriFromFile(const AFile: JFile): Jnet_Uri; overload; static;
|
|
@@ -99,6 +115,15 @@ type
|
|
|
/// </remarks>
|
|
|
class function SharedUriFromFile(const AFileName: string; const AAuthority: string = ''): Jnet_Uri; overload; static;
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// TJnet_Uri.JavaClass.parse
|
|
|
+ /// </summary>
|
|
|
+ class function UriParse(const S: string): Jnet_Uri; overload; static;
|
|
|
+ /// <summary>
|
|
|
+ /// TJnet_Uri.JavaClass.parse
|
|
|
+ /// </summary>
|
|
|
+ class function UriParse(const S: JString): Jnet_Uri; overload; static;
|
|
|
+
|
|
|
/// <summary>Returns Java Application Context</summary>
|
|
|
class property Context: JContext read GetJContext;
|
|
|
/// <summary>Returns Java Application Activity</summary>
|
|
@@ -118,6 +143,8 @@ type
|
|
|
class function GetPackageName: string; static;
|
|
|
/// <summary>Returns MimeType from filename</summary>
|
|
|
class function GetMimeType(AFileName: string): JString; static;
|
|
|
+ /// <summary>Returns Primary SDCard path</summary>
|
|
|
+ class function GetSDCardPath: string; static;
|
|
|
|
|
|
/// <summary>Checks if there is at least one application capable of receiving the intent.</summary>
|
|
|
class function HasAssocApp(const URI: string): Boolean; overload; static;
|
|
@@ -139,9 +166,10 @@ type
|
|
|
implementation
|
|
|
|
|
|
uses
|
|
|
- Androidapi.JNIBridge, Androidapi.JNI.Os, Androidapi.JNI.Support,
|
|
|
+ {$IFDEF DEBUG}FMX.Types,{$ENDIF}
|
|
|
+ Androidapi.JNI.Os, Androidapi.JNI.Support,
|
|
|
Androidapi.JNI.Media, Androidapi.JNI.Provider, Androidapi.JNI.Webkit,
|
|
|
- FMX.Helpers.Android, Androidapi.Helpers;
|
|
|
+ Androidapi.Helpers, FMX.Helpers.Android, System.IOUtils;
|
|
|
|
|
|
{ TAndroidHelperEx }
|
|
|
|
|
@@ -155,6 +183,174 @@ begin
|
|
|
Result := (GetBuildSdkVersion >= AValue) and (GetTargetSdkVersion >= AValue);
|
|
|
end;
|
|
|
|
|
|
+(*
|
|
|
+ * >=4.4
|
|
|
+ * uri=content://com.android.providers.media.documents/document/image%3A293502
|
|
|
+ * uri=file:///storage/emulated/0/temp_photo.jpg
|
|
|
+ * uri=content://media/external/images/media/193968
|
|
|
+ * <4.4
|
|
|
+ * uri=content://media/external/images/media/13
|
|
|
+ * third party
|
|
|
+ * content://com.speedsoftware.explorer.fileprovider/root/storage/emulated/0/Android/data/com.lifan.qspsy/files/cache/thumb/F6AB021A6BBCEFC3B942625FBA2E6ADE/7.jpg
|
|
|
+ * content://com.tencent.mtt.fileprovider/QQBrowser/Movies/BVR_2019_10_14_10_52_29_trimq.mp4
|
|
|
+ * content://com.estrongs.files/storage/emulated/0/DCIM/360%E8%A1%8C%E8%BD%A6%E8%AE%B0%E5%BD%95%E4%BB%AA/2018_02_11_14_16_40_ABAC1BB6.mp4
|
|
|
+ *
|
|
|
+ * 参考文献
|
|
|
+ * https://github.com/coltoscosmin/FileUtils/blob/master/FileUtils.java
|
|
|
+ * https://github.com/DB-BOY/FileChoose/blob/master/app/src/main/java/cn/dbboy/filechoose/FileUtil.java
|
|
|
+ * https://www.jianshu.com/p/c5f207f8cce6
|
|
|
+ * https://blog.csdn.net/chengfu116/article/details/74923161
|
|
|
+ * https://www.cnblogs.com/epmouse/p/5421048.html
|
|
|
+ *
|
|
|
+ * 暂未实现
|
|
|
+ * 安卓10好像完全没有获取真实路径的机会
|
|
|
+ * 安卓7/8/9貌似有些机型还是不行,只能是获取内容后在自己可读写的区域存一份儿
|
|
|
+ * 如果是获取内容而不是path的话,就不需要用这里了,单独搞个api更好
|
|
|
+ *)
|
|
|
+class function TAndroidHelperEx.FileFromUri(const AUri: Jnet_Uri): string;
|
|
|
+
|
|
|
+ function GetUriByType(S: string): Jnet_Uri;
|
|
|
+ begin
|
|
|
+ if SameText(S, 'image') then
|
|
|
+ Result := TJImages_Media.JavaClass.EXTERNAL_CONTENT_URI
|
|
|
+ else if SameText(S, 'video') then
|
|
|
+ Result := TJVideo_Media.JavaClass.EXTERNAL_CONTENT_URI
|
|
|
+ else if SameText(S, 'audio') then
|
|
|
+ Result := TJAudio_Media.JavaClass.EXTERNAL_CONTENT_URI
|
|
|
+ else
|
|
|
+ Result := nil;
|
|
|
+ end;
|
|
|
+
|
|
|
+ function GetDocId(var ADocId, AType, AId: string): Boolean;
|
|
|
+ var
|
|
|
+ LJString: JString;
|
|
|
+ LArr: TArray<string>;
|
|
|
+ begin
|
|
|
+ Result := False;
|
|
|
+ LJString := TJDocumentsContract.JavaClass.getDocumentId(AUri);
|
|
|
+ if LJString = nil then
|
|
|
+ Exit;
|
|
|
+ ADocId := JStringToString(LJString);
|
|
|
+ if ADocId = '' then
|
|
|
+ Exit;
|
|
|
+ LArr := ADocId.Split([':']);
|
|
|
+ if Length(LArr) < 2 then begin
|
|
|
+ AType := '';
|
|
|
+ AId := ADocId;
|
|
|
+ end
|
|
|
+ else begin
|
|
|
+ AType := LArr[0];
|
|
|
+ AId := LArr[1];
|
|
|
+ end;
|
|
|
+ Result := AId <> '';
|
|
|
+ end;
|
|
|
+
|
|
|
+var
|
|
|
+ LSdCard, LPath: string;
|
|
|
+ LAuthority, LScheme, LDocId, LType, LId: string;
|
|
|
+ LSelection: JString;
|
|
|
+ LUri: Jnet_Uri;
|
|
|
+ LArr: TArray<string>;
|
|
|
+ I: Integer;
|
|
|
+ iId: Int64;
|
|
|
+begin
|
|
|
+ Result := '';
|
|
|
+ if AUri = nil then
|
|
|
+ Exit;
|
|
|
+
|
|
|
+ LSdCard := GetSDCardPath;
|
|
|
+ LAuthority := JStringToString(AUri.getAuthority().toString);
|
|
|
+ LScheme := JStringToString(AUri.getScheme());
|
|
|
+ {$IFDEF DEBUG}
|
|
|
+ Log.d('---TAndroidHelperEx.FileFromUri-AUri:%s', [JStringToString(AUri.toString)]);
|
|
|
+ Log.d('---TAndroidHelperEx.FileFromUri-AUri.Authority:%s-LScheme:%s', [LAuthority, LScheme]);
|
|
|
+ {$ENDIF}
|
|
|
+ if CheckBuildAndTarget(KITKAT) and TJDocumentsContract.JavaClass.isDocumentUri(Context, AUri) then begin
|
|
|
+ if not GetDocId(LDocId, LType, LId) then
|
|
|
+ Exit;
|
|
|
+ {$IFDEF DEBUG}
|
|
|
+ Log.d('---TAndroidHelperEx.FileFromUri-LDocId:%s-LType:%s-LId:%s', [LDocId, LType, LId]);
|
|
|
+ {$ENDIF}
|
|
|
+ if SameText(LAuthority, 'com.android.externalstorage.documents') then begin
|
|
|
+ if SameText(LType, 'primary') then
|
|
|
+ Result := TPath.Combine(LSdCard, LId)
|
|
|
+ else if SameText(LType, 'home') then
|
|
|
+ Result := TPath.Combine(TPath.GetSharedDocumentsPath, LId)
|
|
|
+ {$IFDEF DEBUG}
|
|
|
+ else
|
|
|
+ Log.d('---TAndroidHelperEx.FileFromUri-Unkown externalstorage-LDocId:%s-LType:%s-LId:%s', [LDocId, LType, LId]);
|
|
|
+ {$ENDIF}
|
|
|
+ end
|
|
|
+ else if SameText(LAuthority, 'com.android.providers.media.documents') then begin
|
|
|
+ LSelection := StringToJString('_id=' + LId);
|
|
|
+ LUri := GetUriByType(LType);
|
|
|
+ if LUri <> nil then
|
|
|
+ Result := GetDataColumnAsString(LUri, LSelection);
|
|
|
+ end else if SameText(LAuthority, 'com.android.providers.downloads.documents') then begin
|
|
|
+ if SameText(LType, 'raw') then
|
|
|
+ Result := Copy(LDocId, 5)
|
|
|
+ //else if GetBuildSdkVersion < OREO then begin // 有人说O的时候不需要自己解析,但测试锤子手机7.1,不行
|
|
|
+ else begin
|
|
|
+ SetLength(LArr, 2);
|
|
|
+ LArr[0] := 'content://downloads/public_downloads';
|
|
|
+ LArr[1] := 'content://downloads/my_downloads';
|
|
|
+ //LArr[2] := 'content://downloads/all_downloads'; // 这个貌似没权限
|
|
|
+
|
|
|
+ iId := StrToInt64Def(LId, -1);
|
|
|
+ for I := 0 to Length(LArr) - 1 do begin
|
|
|
+ LUri := TJContentUris.JavaClass.withAppendedId(UriParse(LArr[I]), iId);
|
|
|
+ if LUri <> nil then
|
|
|
+ try
|
|
|
+ Result := GetDataColumnAsString(LUri);
|
|
|
+ if Result <> '' then
|
|
|
+ Break;
|
|
|
+ except
|
|
|
+ on E: Exception do begin
|
|
|
+ {$IFDEF DEBUG}
|
|
|
+ Log.d('---TAndroidHelperEx.FileFromUri Error: [%s]%s', [E.ClassName, E.Message]);
|
|
|
+ {$ENDIF}
|
|
|
+ end;
|
|
|
+ end;
|
|
|
+ end;
|
|
|
+ end;
|
|
|
+ //else
|
|
|
+ if Result = '' then
|
|
|
+ Result := GetDataColumnAsString(AUri);
|
|
|
+ end
|
|
|
+ {$IFDEF DEBUG}
|
|
|
+ else
|
|
|
+ Log.d('---TAndroidHelperEx.FileFromUri-Unkown DocumentUri-');
|
|
|
+ {$ENDIF}
|
|
|
+ end else if SameText(LScheme, 'content') then begin
|
|
|
+ // Return the remote address
|
|
|
+ if SameText(LAuthority, 'com.google.android.apps.photos.content') then
|
|
|
+ Result := JStringToString(AUri.getLastPathSegment())
|
|
|
+ else if SameText(LAuthority, 'com.tencent.mtt.fileprovider') then begin
|
|
|
+ LPath := JStringToString(AUri.getPath());
|
|
|
+ if Pos('/QQBrowser/', LPath) = 1 then begin // /QQBrowser/XXX
|
|
|
+ LPath := TPath.Combine(LSdCard, Copy(LPath, 12));
|
|
|
+ if FileExists(LPath) then
|
|
|
+ Result := LPath;
|
|
|
+ end;
|
|
|
+ end
|
|
|
+ else if SameText(LAuthority, 'com.speedsoftware.explorer.fileprovider') then begin
|
|
|
+ LPath := JStringToString(AUri.getPath());
|
|
|
+ if Pos('/root/', LPath) = 1 then
|
|
|
+ Result := Copy(LPath, 6);
|
|
|
+ end
|
|
|
+ else if SameText(LAuthority, 'com.estrongs.files') then
|
|
|
+ Result := JStringToString(AUri.getPath())
|
|
|
+ else
|
|
|
+ Result := GetDataColumnAsString(AUri);
|
|
|
+ end
|
|
|
+ else if SameText(LScheme, 'file') then
|
|
|
+ Result := JStringToString(AUri.getPath())
|
|
|
+ {$IFDEF DEBUG}
|
|
|
+ else
|
|
|
+ Log.d('---TAndroidHelperEx.FileFromUri-Unkown Uri-');
|
|
|
+ {$ENDIF}
|
|
|
+end;
|
|
|
+
|
|
|
class function TAndroidHelperEx.GetBuildSdkVersion: Integer;
|
|
|
begin
|
|
|
Result := TJBuild_VERSION.JavaClass.SDK_INT;
|
|
@@ -165,6 +361,46 @@ begin
|
|
|
Result := TJLang_Class.JavaClass.forName(StringToJString(APackageClassName), True, Context.getClassLoader);
|
|
|
end;
|
|
|
|
|
|
+class function TAndroidHelperEx.GetColumnAsString(const AUri: Jnet_Uri; const AColumn: JString;
|
|
|
+ ASelection: JString; ASelectionArgs: TJavaObjectArray<JString>; ASortOrder: JString): string;
|
|
|
+var
|
|
|
+ LCursor: JCursor;
|
|
|
+ LIndex: Integer;
|
|
|
+ LPojection: TJavaObjectArray<JString>;
|
|
|
+begin
|
|
|
+ Result := '';
|
|
|
+ if AUri = nil then
|
|
|
+ Exit;
|
|
|
+
|
|
|
+ LPojection := TJavaObjectArray<JString>.Create(1);
|
|
|
+ LPojection.Items[0] := AColumn;
|
|
|
+ LCursor := Context.getContentResolver().query(AUri, LPojection, ASelection, ASelectionArgs, ASortOrder);
|
|
|
+ if LCursor = nil then begin
|
|
|
+ {$IFDEF DEBUG}
|
|
|
+ Log.d('---TAndroidHelperEx.GetColumnAsString-LCursor = nil-');
|
|
|
+ {$ENDIF}
|
|
|
+ Exit;
|
|
|
+ end;
|
|
|
+ try
|
|
|
+ LIndex := LCursor.getColumnIndex(AColumn);
|
|
|
+ if (LIndex > -1) and LCursor.moveToFirst then
|
|
|
+ Result := JStringToString(LCursor.getString(LIndex))
|
|
|
+ {$IFDEF DEBUG}
|
|
|
+ else
|
|
|
+ Log.d('---TAndroidHelperEx.GetColumnAsString-LIndex:%d-', [LIndex]);
|
|
|
+ {$ENDIF}
|
|
|
+ finally
|
|
|
+ LCursor.close;
|
|
|
+ end;
|
|
|
+end;
|
|
|
+
|
|
|
+class function TAndroidHelperEx.GetDataColumnAsString(const AUri: Jnet_Uri;
|
|
|
+ ASelection: JString; ASelectionArgs: TJavaObjectArray<JString>;
|
|
|
+ ASortOrder: JString): string;
|
|
|
+begin
|
|
|
+ Result := GetColumnAsString(AUri, StringToJString('_data'), ASelection, ASelectionArgs, ASortOrder);
|
|
|
+end;
|
|
|
+
|
|
|
class function TAndroidHelperEx.GetDefaultIconID: Integer;
|
|
|
begin
|
|
|
Result := Context.getApplicationInfo.icon;
|
|
@@ -228,6 +464,17 @@ begin
|
|
|
Result := JStringToString(Context.getPackageName);
|
|
|
end;
|
|
|
|
|
|
+class function TAndroidHelperEx.GetSDCardPath: string;
|
|
|
+var
|
|
|
+ sPath: string;
|
|
|
+begin
|
|
|
+ Result := '';
|
|
|
+ sPath := System.IOUtils.TPath.GetSharedDocumentsPath;
|
|
|
+ if Pos(PathDelim, sPath) = 0 then
|
|
|
+ Exit;
|
|
|
+ Result := ExtractFilePath(ExcludeTrailingPathDelimiter(sPath));
|
|
|
+end;
|
|
|
+
|
|
|
class function TAndroidHelperEx.GetTargetSdkVersion: Integer;
|
|
|
var
|
|
|
LApplicationInfo: JApplicationInfo;
|
|
@@ -253,7 +500,7 @@ var
|
|
|
begin
|
|
|
Result := False;
|
|
|
Intent := TJIntent.Create;
|
|
|
- Intent.setData(TJnet_Uri.JavaClass.parse(StringToJString(URI)));
|
|
|
+ Intent.setData(UriParse(URI));
|
|
|
Intent.setAction(StringToJString('android.intent.action.VIEW'));
|
|
|
Result := HasAssocApp(Intent);
|
|
|
end;
|
|
@@ -344,6 +591,16 @@ begin
|
|
|
end;
|
|
|
end;
|
|
|
|
|
|
+class function TAndroidHelperEx.UriParse(const S: JString): Jnet_Uri;
|
|
|
+begin
|
|
|
+ Result := TJnet_Uri.JavaClass.parse(S);
|
|
|
+end;
|
|
|
+
|
|
|
+class function TAndroidHelperEx.UriParse(const S: string): Jnet_Uri;
|
|
|
+begin
|
|
|
+ Result := UriParse(StringToJString(S));
|
|
|
+end;
|
|
|
+
|
|
|
class function TAndroidHelperEx.UriFromFile(const AFileName: string): Jnet_Uri;
|
|
|
begin
|
|
|
Result := UriFromFile(TJFile.JavaClass.init(StringToJString(AFileName)));
|