Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

Android java : How to create folder inside android devices?

I was developing an app and needed to create a folder inside SD card when button is onClick. I have no idea how to do it. Anyone can help ? Please.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_date_select);
    edit_date = (EditText) findViewById(R.id.edit_date);
    edit_date.setGravity(Gravity.CENTER_HORIZONTAL);
    btn_setDate = (Button) findViewById(R.id.btn_setDate);
   btn_setDate.setOnClickListener(this);
    updateDate();

    btn_cFolder = (Button) findViewById(R.id.btn_cFolder);
     btn_cFolder.setOnClickListener(new OnClickListener() {
         public void onClick(View v) 
            {
             Environment.getExternalStorageDirectory();
             File folder = new File(Environment.getExternalStorageDirectory() + "/wahaha");
             boolean success = true;
             if (!folder.exists()) {
                 success = folder.mkdir();
             }
             if (success) {
                 Toast.makeText(getBaseContext(), "You have successfully created." , Toast.LENGTH_LONG ).show();
             } else {
                 Toast.makeText(getBaseContext(), "You have Failed to create." , Toast.LENGTH_LONG ).show();
             }
            }
   });

}

I'm beginner of android develop. okay i now have doing many of research and i found out the folder are able to see on the android device. But once i plugged into PC . still not able to see. Any suggestion see the folder on PC? And by the way why it save to internal storage ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

3 step:

  1. To get sd card is mounted at /sdcard or any other location by using this way:

    Environment.getExternalStorageDirectory();
    
  2. You have to take uses-permission entry in the AndroidManifest.xml file:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
  3. If directory already exists, then mkdir return false. Try creatinng new directory if it not exist:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...