/** * 创建文件夹 * <p> * getFilesDir()不能使用安装目录,“解析程序包时出现问题” * Environment.getRootDirectory() 无法更改文件读写权限 * getCacheDir()不能再此目录创建文件,再放入apk,也会提示“解析程序包时出现问题” */ public static String createFilePath(Context context, String s) { String path; if (isSdcard()) { path = Environment.getExternalStorageDirectory() + "/YJY/Save/" + s + "/"; File file = new File(path); if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } } else { path = context.getCacheDir() + "/"; File file = new File(path); chmod("777", file.getAbsolutePath()); if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } } return path; } /** * 修改apk权限 */ public static void chmod(String permission, String path) { try { String command = "chmod " + permission + " " + path; Runtime runtime = Runtime.getRuntime(); runtime.exec(command); } catch (IOException e) { Log.e("akui", "修改apk权限失败"); } } /** * 是否存在sdcard */ public static boolean isSdcard() { try { return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); } catch (Exception e) { e.printStackTrace(); } return false; } /** * 打开文件进行安装 */ public static void openFiles(Context context, File file) { chmod("777", file.getAbsolutePath()); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); context.startActivity(intent); }
获取文件路径
Environment.getDataDirectory() = /data
Environment.getDownloadCacheDirectory() = /cache
Environment.getExternalStorageDirectory() = /mnt/sdcard
Environment.getExternalStoragePublicDirectory(“test”) = /mnt/sdcard/test
Environment.getRootDirectory() = /system
getPackageCodePath() = /data/app/com.my.app-1.apk
getPackageResourcePath() = /data/app/com.my.app-1.apk
getCacheDir() = /data/data/com.my.app/cache
getDatabasePath(“test”) = /data/data/com.my.app/databases/test
getDir(“test”, Context.MODE_PRIVATE) = /data/data/com.my.app/app_test
getExternalCacheDir() = /mnt/sdcard/Android/data/com.my.app/cache
getExternalFilesDir(“test”) = /mnt/sdcard/Android/data/com.my.app/files/test
getExternalFilesDir(null) = /mnt/sdcard/Android/data/com.my.app/files
getFilesDir() = /data/data/com.my.app/files