使用Delphi编程程序来检查SD卡是否可用。

4

我正在使用Delphi 10 Seattle开发移动应用程序。

我需要使用Delphi 10 Seattle编写程序来检查设备中是否可用SD卡?

我已经找到了一些使用JAVA的示例。请提供一些使用Delphi的示例。


看起来非常简单。你试过了吗? - Free Consulting
你可以自己将那段示例代码转换为Delphi。如果有什么不起作用的地方,可以在这里提问。 - Jan Doggen
1个回答

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接