Android: Create a file or folder and get the SD card root directory

Source: Internet
Author: User

Directory structure:

function, you can generate the corresponding files or folders according to the input directory or folder.

First you need to add a permission:

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

Mainactivity.java:

Package Com.wyl.xml;import Com.wyl.download.fileutils;import Android.app.activity;import android.os.Bundle;import Android.view.view;import Android.widget.button;import Android.widget.edittext;import Android.widget.Toast;public Class Mainactivity extends Activity {Button btn; Button btn2; EditText TV; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); btn = (Button) Findviewbyid (r.id.btn01); btn2 = (Button) Findviewbyid ( R.id.createfile); TV = (EditText) Findviewbyid (R.ID.TV01);} public void Doonclick (view view) {FileUtils util = new FileUtils (); switch (View.getid ()) {//Get root case R.id.btn01:btn.sett Ext (Util.getsdcardpath ()); break;//Create folder or file case r.id.createfile:string FileName = Tv.gettext (). toString (); Util.createfile (FileName); Mytoast ("Create file or folder" +filename+ "Success", "N"); /** * Package system provides Toast * @param msg Prompt content * @param time Click to prompt for the interval, in milliseconds */public void Mytoast (String msg,int time) {Toast.mak EText (This, MSg, time). Show ();}} 

Tool class:

Fileutils.java:

Package Com.wyl.download;import Java.io.file;import Java.io.ioexception;import Android.os.environment;public class FileUtils {private static string sdpath = "";/** * get to the root of the SD card and return as String *  * @return */public static string Getsdcar Dpath () {Sdpath = Environment.getexternalstoragedirectory () + "/"; return Sdpath;} /** * Create file or folder *  * @param filename *            file name or ask folder name */public void CreateFile (String filename) {File File = new file (get Sdcardpath () + fileName), if (Filename.indexof (".")! =-1) {//description contains, even if the file is created, the return value of 1 is not included. Even if the file try {file.createnewfile ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} SYSTEM.OUT.PRINTLN ("created file");} else {//Create folder File.mkdir (); System.out.println ("Created folder");}}}

Layout file:

Activity_main.xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/container "android:layout_width=" Match_parent "android:layout_height= "Match_parent" tools:context= "com.wyl.xml.MainActivity" tools:ignore= "Mergerootframe" > <button and Roid:id= "@+id/btn01" android:onclick= "Doonclick" android:layout_width= "Wrap_content" Android:layout_h        eight= "Wrap_content" android:text= "Get Root"/> <edittext android:layout_below= "@id/btn01" Android:id= "@+id/tv01" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" a Ndroid:hint= "The name of the file or folder to be created, you need to add the suffix"/> <button android:layout_below= "@id/tv01" android:id= "@+ Id/createfile "android:onclick=" Doonclick "android:layout_width=" Wrap_content "android:layout_height=    "Wrap_content" android:text= "Create File"    /></relativelayout> 

  

  

Android: Create a file or folder and get the SD card root directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.