简体   繁体   English

如何确定我的应用程序是否安装在 SD 卡上

[英]How can i find out if my app is installed on SD card

I would like something do something like:我想做一些类似的事情:

val cacheDir = if (installedOnSD)
   {
   getContext.getExternalCacheDir
   }
else
   {
   getContext.getCacheDir
   }

and I am a bit at a loss for the installedOnSD part.我对installedOnSD部分有点不知所措。 Can anybody point me to the right direction?谁能指出我正确的方向?

PS: Pseudo-Code sample in Scala, just for the fun of it. PS:Scala中的伪代码示例,只是为了好玩。

Here is my code for checking if app is installed on SD card:这是我检查应用程序是否安装在 SD 卡上的代码:

 /**
   * Checks if the application is installed on the SD card.
   * 
   * @return <code>true</code> if the application is installed on the sd card
   */
  public static boolean isInstalledOnSdCard() {

    Context context = App.getContext();
    // check for API level 8 and higher
    if (VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
      PackageManager pm = context.getPackageManager();
      try {
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        ApplicationInfo ai = pi.applicationInfo;
        return (ai.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE;
      } catch (NameNotFoundException e) {
        // ignore
      }
    }

    // check for API level 7 - check files dir
    try {
      String filesDir = context.getFilesDir().getAbsolutePath();
      if (filesDir.startsWith("/data/")) {
        return false;
      } else if (filesDir.contains("/mnt/") || filesDir.contains("/sdcard/")) {
        return true;
      }
    } catch (Throwable e) {
      // ignore
    }

    return false;
  }

To Check application is installed in SD Card or not, just do this:要检查应用程序是否安装在 SD 卡中,只需执行以下操作:

ApplicationInfo io = context.getApplicationInfo();

if(io.sourceDir.startsWith("/data/")) {

//application is installed in internal memory
return false;

} else if(io.sourceDir.startsWith("/mnt/") || io.sourceDir.startsWith("/sdcard/")) {

//application is installed in sdcard(external memory)
return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作我的应用程序以便将其安装在SD卡上? - How can I make my app so it can be installed on the sd card? Android - 如何将我的应用程序安装到SD卡 - Android - How can i install my app to sd card 如何查看应用程序是否已移至Android上的SD卡 - How to find out if an App has been moved to SD card on Android 如何在Android首次运行应用程序时将文件从我的资产文件夹复制到我的SD卡? - How can I copy a file from my assets folder into my SD card at first run of the app in Android? 如何使用Soundpool从Android应用程序中原始文件夹插入的SD卡中加载声音? - How can I load a sound from the SD Card insted of the raw folder in my Android app useing Soundpool? 如何从SD卡加载pdf文件 - How can i load my pdf file from sd card 有没有办法将要安装在SD卡上的视频文件保留在我的应用程序中? - Is there a way to keep video files within my app that will be installed on the sd card? 如果我的应用程序安装在SD卡上,私人数据也在那里吗? - If my app's installed on the SD card, is the private data there too? 如何将我的数据库保存在 SD 卡上并使用 ORMLite? - How can I keep my database on an SD card and use ORMLite? 我可以使用已安装到SD卡的应用程序小部件吗 - Can I use widget of application that installed to sd-card
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM