Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 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 Answer

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 
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...