【问题标题】:Cannot find directory at boot启动时找不到目录
【发布时间】:2012-02-10 15:53:09
【问题描述】:

在我的应用程序中,我创建了一个旨在从 sd 卡读取内容的服务。

服务在启动时创建并启动。

问题是虽然我很确定目录存在,但在启动时,服务找不到目录。

public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    Context context=getBaseContext();
    File sdDir = new File(Environment.getExternalStorageDirectory()+"/temp/Data/");
    if(!sdDir.exists()){
        sdDir.mkdir();
        Toast.makeText(this, "CAN'T FIND!", Toast.LENGTH_LONG).show();
    }
 }

上面这个sn-p输出

  1. 服务已启动
  2. 找不到!

起初,我认为可能在启动时没有挂载 sd 卡,这就是服务找不到目录的原因。我仍然不确定。

有人有想法吗?可能是什么问题?

【问题讨论】:

    标签: android file boot android-sdcard


    【解决方案1】:

    有些设备需要一些时间来安装 SD 卡。它可能不会在启动时间后立即可用。

    每隔几秒轮询一次,直到可用为止。

    也试试这个:

    public static boolean hasStorage(boolean requireWriteAccess) {  
        String state = Environment.getExternalStorageState();  
    
        if (Environment.MEDIA_MOUNTED.equals(state)) {  
            return true;  
        } else if (!requireWriteAccess  && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {  
            return true;  
        }  
        return false;  
    }
    

    From Here

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2014-03-14
      • 2021-02-21
      • 2011-06-27
      • 1970-01-01
      相关资源
      最近更新 更多