3

before I used getExternalCacheDir() to get ext cache dir path for cache images, but sometime I clicked "Clear data" from Setting-AppInfo,then getExternalCacheDir() return null, because all package folders were removed from "Android/data ",So I think I can generate it first ,but I tried some ways I cant do this successfully, can anyone teach me how to do ? why I cant create this directory? and in this case, how to do will be more elegant solution?

thanks!

      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();
          }
    }

Before I have added

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Elan
  • 31
  • 1
  • 4

2 Answers2

1

Add the following code to checkOrRebaseAppExtCacheDir() method.

         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();
          }
Ram
  • 2,532
  • 4
  • 22
  • 25
0

I use this in my Android project

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"
      );
    }
  }
}

And then something like

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

context is your acivity, application or getContext() in view

Dmitry Kolesnikovich
  • 669
  • 2
  • 15
  • 33
  • thank you, and before I hava added this permission, do u know why I cant create that directory? – Elan Dec 16 '13 at 08:56
  • That's what permission is about: if you don't trust this developer you can avoid installing his app. So as a developer you should _explicitly_ define app permissions in AndroidManifest.xml file. There you can find all permissions: http://developer.android.com/reference/android/Manifest.permission.html – Dmitry Kolesnikovich Dec 16 '13 at 18:41
  • The second possible answer is: if failed root.mkdirs() just returns false but not throws an exception. This is example of silent fail. – Dmitry Kolesnikovich Dec 16 '13 at 18:58