技術はメシのタネ

底辺プログラマで技術の向上を目指しているけれどやりたい事が無くて困っている

ちょっとメモ。

public class LocalStorageManager {
    private static final int mPrivateMode = Context.MODE_PRIVATE;
    private static final int mPublicMode  = Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE;
    private static final String prefix = "app_";
    
    /**
    * /data/data/パッケージ名を取得する
    * @param context
    * @return
    * @throws FileNotFoundException 
    */
    public static final File getPackageNameDirectory (Context context) throws FileNotFoundException {
        String temporary = "hoge";
        StringBuilder builder = new StringBuilder();
        builder.append(prefix).append(temporary);
        
        File root = new File(context.getDir(temporary, mPrivateMode).getAbsolutePath().replace(builder.toString(), ""));
        if (!root.exists()) {
            throw new FileNotFoundException();
        }
        return root;
    }

    /**
    * /data/data/パッケージ名/直下にpathフォルダがあるかどうか
    * @param context
    * @param path app_"path"ディレクトリ名 
    * @return
    */
    public static final File findLocalDirectory (Context context, final String path) {
        try {
            File find = new File(getPackageNameDirectory(context), path);
            if (find.exists()) {
                return find;
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}