01.
package
com.myapp;
02.
03.
import
java.io.File;
04.
import
java.io.IOException;
05.
import
android.os.Bundle;
06.
import
android.app.Activity;
07.
import
android.view.Menu;
08.
import
android.view.View;
09.
import
android.widget.Button;
10.
import
android.widget.EditText;
11.
import
android.widget.Toast;
12.
13.
public
class
MainActivity
extends
Activity {
14.
15.
@Override
16.
public
void
onCreate(Bundle savedInstanceState) {
17.
super
.onCreate(savedInstanceState);
18.
setContentView(R.layout.activity_main);
19.
20.
21.
final
EditText txtFilename = (EditText) findViewById(R.id.editText1);
22.
23.
24.
final
Button btn1 = (Button) findViewById(R.id.button1);
25.
26.
27.
btn1.setOnClickListener(
new
View.OnClickListener() {
28.
public
void
onClick(View v) {
29.
30.
31.
try
{
32.
33.
String path = txtFilename.getText().toString();
34.
File file =
new
File(path);
35.
36.
if
(file.exists())
37.
{
38.
file.delete();
39.
}
40.
41.
file =
null
;
42.
43.
44.
Toast.makeText(MainActivity.
this
,
"File Delete!"
,
45.
Toast.LENGTH_LONG).show();
46.
47.
}
catch
(IOException e) {
48.
49.
e.printStackTrace();
50.
Toast.makeText(MainActivity.
this
,
"Failed! = "
+ e.getMessage() ,
51.
Toast.LENGTH_LONG).show();
52.
}
53.
54.
55.
}
56.
});
57.
58.
59.
60.
}
61.
62.
@Override
63.
public
boolean
onCreateOptionsMenu(Menu menu) {
64.
getMenuInflater().inflate(R.menu.activity_main, menu);
65.
return
true
;
66.
}
67.
68.
}