我尝试这样做,当用户第一次启动应用时,会在手机的Gallery部分创建一个新文件夹,并且每次启动应用时都会复制ISN&#T; T。我在这里看了一些答案,但大多数都是混乱或过时的。请有人帮忙吗?
我尝试了以下内容,但它似乎无法运作:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
final File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "MyNewFolderName" + File.separator)
};
我还在AndroidManifext.xml中包含了这一行:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 0 :(得分:4)
你是如此接近答案:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
// this line creates reference to a folder that might or not exist, but does not create
File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "MyNewFolderName" + File.separator)
root.mkdirs(); // this line creates the folder
};
另外,如果你在Marshmallow(android 6)上运行它,你必须首先请求允许写入存储,你可以在这里阅读有关请求权限http://developer.android.com/training/permissions/index.html