使用Delphi以编程方式检查SD卡是否可用

时间:2016-01-11 06:53:10

标签: android delphi firemonkey delphi-10-seattle

我正在使用Delphi 10 Seattle开发移动应用程序。 我需要以编程方式检查SD卡是否在使用Delphi 10 Seattle的设备中可用?

我使用JAVA找到了一些示例。请给我一些使用Delphi的例子

1 个答案:

答案 0 :(得分:5)

Delphi没有为Android Environment类定义接口,但您可以在自己的代码中手动定义它,例如:

uses
  ...,
  Androidapi.Helpers,
  Androidapi.JNIBridge,
  Androidapi.JNI.JavaTypes;

type
  JEnvironment = interface;

  JEnvironmentClass = interface(JObjectClass)
    ['{D131F4D4-A6AD-43B7-B2B6-A9222BC46C74}']
    function _GetMEDIA_BAD_REMOVAL: JString; cdecl;
    function _GetMEDIA_CHECKING: JString; cdecl;
    function _GetMEDIA_EJECTING: JString; cdecl;
    function _GetMEDIA_MOUNTED: JString; cdecl;
    function _GetMEDIA_MOUNTED_READ_ONLY: JString; cdecl;
    function _GetMEDIA_NOFS: JString; cdecl;
    function _GetMEDIA_REMOVED: JString; cdecl;
    function _GetMEDIA_SHARED: JString; cdecl;
    function _GetMEDIA_UNKNOWN: JString; cdecl;
    function _GetMEDIA_UNMOUNTABLE: JString; cdecl;
    function _GetMEDIA_UNMOUNTED: JString; cdecl;
    function _GetDIRECTORY_ALARMS: JString; cdecl;
    function _GetDIRECTORY_DCIM: JString; cdecl;
    function _GetDIRECTORY_DOCUMENTS: JString; cdecl;
    function _GetDIRECTORY_DOWNLOADS: JString;
    function _GetDIRECTORY_MOVIES: JString; cdecl;
    function _GetDIRECTORY_MUSIC: JString; cdecl;
    function _GetDIRECTORY_NOTIFICATIONS: JString; cdecl;
    function _GetDIRECTORY_PICTURES: JString; cdecl;
    function _GetDIRECTORY_PODCASTS: JString; cdecl;
    function _GetDIRECTORY_RINGTONES: JString; cdecl;
    {class} function init: JEnvironment; cdecl;
    {class} function getDataDirectory: JFile; cdecl; 
    {class} function getDownloadCacheDirectory: JFile; cdecl;
    {class} function getExternalStorageDirectory(): JFile; cdecl;
    {class} function getExternalStoragePublicDirectory(type: JString): JFile; cdecl;
    {class} function getExternalStorageState(path: JFile): JString; cdecl; 
    {class} function getExternalStorageState: JString; cdecl;
    {class} function getRootDirectory: JFile; cdecl;
    {class} function getStorageState(path: JFile): JString; cdecl; 
    {class} function isExternalStorageEmulated: Boolean; cdecl;
    {class} function isExternalStorageEmulated(path: JFile): Boolean; cdecl;
    {class} function isExternalStorageRemovable(path: JFile): Boolean; cdecl;
    {class} function isExternalStorageRemovable: Boolean; cdecl;
    {class} property MEDIA_BAD_REMOVAL: JString read _GetMEDIA_BAD_REMOVAL;
    {class} property MEDIA_CHECKING: JString read _GetMEDIA_CHECKING;
    {class} property MEDIA_EJECTING: JString read _GetMEDIA_EJECTING;
    {class} property MEDIA_MOUNTED: JString read _GetMEDIA_MOUNTED;
    {class} property MEDIA_MOUNTED_READ_ONLY: JString read _GetMEDIA_MOUNTED_READ_ONLY;
    {class} property MEDIA_NOFS: JString read _GetMEDIA_NOFS;
    {class} property MEDIA_REMOVED: JString read _GetMEDIA_REMOVED;
    {class} property MEDIA_SHARED: JString read _GetMEDIA_SHARED;
    {class} property MEDIA_UNKNOWN: JString read _GetMEDIA_UNKNOWN;
    {class} property MEDIA_UNMOUNTABLE: JString read _GetMEDIA_UNMOUNTABLE;
    {class} property MEDIA_UNMOUNTED: JString read _GetMEDIA_UNMOUNTED;
    {class} property DIRECTORY_ALARMS: JString read _GetDIRECTORY_ALARMS;
    {class} property DIRECTORY_DCIM: JString read _GetDIRECTORY_DCIM;
    {class} property DIRECTORY_DOCUMENTS: JString read _GetDIRECTORY_DOCUMENTS;
    {class} property DIRECTORY_DOWNLOADS: JString read _GetDIRECTORY_DOWNLOADS;
    {class} property DIRECTORY_MOVIES: JString read _GetDIRECTORY_MOVIES;
    {class} property DIRECTORY_MUSIC: JString read _GetDIRECTORY_MUSIC;
    {class} property DIRECTORY_NOTIFICATIONS: JString read _GetDIRECTORY_NOTIFICATIONS;
    {class} property DIRECTORY_PICTURES: JString read _GetDIRECTORY_PICTURES;
    {class} property DIRECTORY_PODCASTS: JString read _GetDIRECTORY_PODCASTS;
    {class} property DIRECTORY_RINGTONES: JString read _GetDIRECTORY_RINGTONES;
  end;

  [JavaSignature('android/os/Environment')]
  JEnvironment = interface(JObject)
    ['{83A2E94E-7D8E-432F-BE21-AEC2115015BE}']
  end;

  TJEnvironment = class(TJavaGenericImport<JEnvironmentClass, JEnvironment>);

然后你可以这样做:

type
  SdCardState = (SdcardWritable, SdcardReadOnly, SdcardNotAvailable);

function GetSdcardState: SdCardState;
var
  state: JString;
begin
  state := TJEnvironment.JavaClass.getExternalStorageState;
  if state.equals(TJEnvironment.JavaClass.MEDIA_MOUNTED) then
    Result := SdcardWritable
  else if state.equals(TJEnvironment.JavaClass.MEDIA_MOUNTED_READ_ONLY) then
    Result := SdcardReadOnly
  else
    Result := SdcardNotAvailable;
end;

function GetSdcardPath: String;
var
  vFile: JFile;
begin
  vFile := TJEnvironment.JavaClass.getExternalStorageDirectory;
  Result := JStringToString(vFile.getAbsolutePath);
end;