<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
String path = "/mnt/sdcard/mypicture/" File folder = new File(path); folder.mkdir();
String path = "/mnt/sdcard/mypicture/" File folder = new File(path); folder.delete();
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="33dp" android:text="Input Directory" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" android:ems="20" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="44dp" android:text="Create" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="33dp" android:text="Delete" /> </RelativeLayout>
package com.myapp; import java.io.File; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // editText1 Folder name final EditText txtFoldername = (EditText) findViewById(R.id.editText1); // button1 (Create) final Button btn1 = (Button) findViewById(R.id.button1); // button2 (Delete) final Button btn2 = (Button) findViewById(R.id.button2); // Perform action on click (Create) btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { /*** Create Directory in SD Card ***/ try { String path = txtFoldername.getText().toString(); File folder = new File(path); if(!folder.exists()) { folder.mkdir(); } folder = null; Toast.makeText(MainActivity.this, "Directory Create!", Toast.LENGTH_LONG).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(MainActivity.this, "Failed! = " + e.getMessage() , Toast.LENGTH_LONG).show(); } } }); // Perform action on click (Delete) btn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { /*** Create Directory in SD Card ***/ try { String path = txtFoldername.getText().toString(); File folder = new File(path); if(!folder.exists()) { folder.delete(); } folder = null; Toast.makeText(MainActivity.this, "Directory Delete!", Toast.LENGTH_LONG).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(MainActivity.this, "Failed! = " + e.getMessage() , Toast.LENGTH_LONG).show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท