该文件夹未显示

时间:2017-12-29 05:17:07

标签: android

 gbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            File folder=new File(Environment.getExternalStorageDirectory()+"/test");
            boolean sucess =true;
            if(!folder.exists())
            {
                sucess=folder.mkdirs();
            }
            if(sucess)
            {
                Toast.makeText(getApplicationContext(),"created",Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(),"not created",Toast.LENGTH_LONG).show();
            }






        }
    });

我试过这段代码

我的应用需要一个文件夹,需要在文件资源管理器中显示,帮助我创建公共目录

2 个答案:

答案 0 :(得分:0)

File folder = new File(Environment.getExternalStorageDirectory() + "/test");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
// Do something on success
} else {
// Do something else on failure 
}

在Manifest中添加此权限,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

答案 1 :(得分:0)

在Manufest文件中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

编写此代码

 File root = android.os.Environment.getExternalStorageDirectory();
 File dir = new File(root.getAbsolutePath() + "/test");
 if(!dir.exists()){
    dir.mkdirs();
 }