Android读写内存

GA666666 2021-05-10 PM 36℃ 0条

由于安卓更新7.0以后对各种权限进行了控制,所以原方法不能使用
如使用:Environment.getExternalStorageDirectory() 会报open failed: EPERM (Operation not permitted)的错误

7.0以后使用:

ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getExternalFilesDir(Environment.DIRECTORY_MUSIC);
File file = new File(directory, "something" + ".MP3");

这里的
Environment.DIRECTORY_MUSIC还有其他形式:

public static String DIRECTORY_MUSIC = "Music";
public static String DIRECTORY_ALARMS = "Alarms";
public static String DIRECTORY_NOTIFICATIONS = "Notifications";
public static String DIRECTORY_PICTURES = "Pictures";
public static String DIRECTORY_MOVIES = "Movies";
public static String DIRECTORY_DOWNLOADS = "Download";
public static String DIRECTORY_DCIM = "DCIM";
public static String DIRECTORY_DOCUMENTS = "Documents";

标签: none

非特殊说明,本博所有文章均为博主原创。

上一篇 C语言二叉树
下一篇 Java 迷宫问题

评论啦~