如何在设置“清除数据”后在SD卡上创建文件夹“Android / data / your_package / cache”?

时间:2013-12-16 07:27:49

标签: android caching

之前我使用getExternalCacheDir()获取缓存图像的ext cache dir路径,但有时我点击了Setting-AppInfo中的“Clear data”,然后getExternalCacheDir()返回null,因为所有包文件夹都从“Android / data”中删除“所以我想我可以先生成它,但我尝试了一些方法,我不能成功地做到这一点,任何人都可以教我怎么做?为什么我不能创建这个目录?而在这种情况下,如何做到更优雅的解决方案?

谢谢!

      public static String getAppCacheStorageDir(Context context,String folder) {

//      http://stackoverflow.com/questions/10123812/diff-between-getexternalfilesdir-and-getexternalstoragedirectory
//      http://stackoverflow.com/questions/16562165/getexternalcachedir-returns-null-after-clearing-data        

//      final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ?
//      context.getExternalCacheDir().getPath() : context.getCacheDir().getPath();
////    return cachePath + File.separator + context.getPackageName()+ File.separator +folder;
//    return cachePath + File.separator +folder;

      boolean isExtStorageMounted = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ?
                                    true: false;
      if(isExtStorageMounted){
          if(checkOrRebaseAppExtCacheDir(context)){
              return context.getExternalCacheDir().getPath()+ File.separator +folder; 
          }else{
              return context.getCacheDir().getPath() + File.separator +folder; 
          }
      }

      return context.getCacheDir().getPath() + File.separator +folder; 

    }



    public static boolean checkOrRebaseAppExtCacheDir(Context context){

      String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ?
              Environment.getExternalStorageDirectory().getPath()
              +File.separator+"Android"
              +File.separator+"data"
              +File.separator+context.getPackageName()
              +File.separator+"cache"
              : 
              null;
      if(cachePath!=null){
          File file = new File(cachePath);
          if (!file.exists()) {
              getPermissionForAppExtCacheDir(context);
              return file.mkdirs();
          }else{ return true; }
      }else{
          return false;
      }



    }
    public static void getPermissionForAppExtCacheDir(Context context)
    {
          String command = "chmod 777 " 
                  +  
                  Environment.getExternalStorageDirectory().getPath()
                  +File.separator+"Android"
                  +" "+
                  Environment.getExternalStorageDirectory().getPath()
                  +File.separator+"Android"
                  +File.separator+"data"
                  +" "+
                  Environment.getExternalStorageDirectory().getPath()
                  +File.separator+"Android"
                  +File.separator+"data"
                  +File.separator+context.getPackageName()
                  +" "+
                  Environment.getExternalStorageDirectory().getPath()
                  +File.separator+"Android"
                  +File.separator+"data"
                  +File.separator+context.getPackageName()
                  +File.separator+"cache"
                  ;
          Runtime runtime = Runtime.getRuntime();
          try {
            runtime.exec(command);
          } catch (IOException e) {
            e.printStackTrace();
          }
    }

在我添加之前

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2 个答案:

答案 0 :(得分:1)

将以下代码添加到checkOrRebaseAppExtCacheDir()方法。

         try{
          File root = new File(Environment.getExternalStorageDirectory(), "/Android/data/"+packageName+"/cache/");
               if (!root.exists()) {
                   root.mkdirs();
                }
                //Do your stuff.
          }
          catch(Exception e){
                  Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_SHORT).show();
          }

答案 1 :(得分:0)

我在我的Android项目中使用它

public static void createDirectory(File dir) throws IllegalStateException{
  if (!dir.exists()){
    if(!dir.mkdirs()){
      throw new IllegalStateException(
        "Check if you've added permissions in AndroidManifest.xml: \n" +
        "<uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\"/> \n"
      );
    }
  }
}

然后像

File sdDir = Environment.getExternalStorageDirectory();
String packageName = context.getPackageName();
File dir = new File(sdDir, "/Android/data/"+packageName+"/cache/");
createDirectory(dir);

context是您在视图中的活动,应用程序或getContext()