Skip to content

Commit 92cd40e

Browse files
committed
File I/O activity and Sound activity
1 parent cd0cfd0 commit 92cd40e

File tree

10 files changed

+117
-7
lines changed

10 files changed

+117
-7
lines changed

AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@
3434
android:name="com.example.helloworld.PreferenceActivity"
3535
android:label="Preference Activity" >
3636
</activity>
37-
<activity
37+
<activity
3838
android:name="com.example.helloworld.TextFileActivity"
3939
android:label="TextFile Activity" >
4040
</activity>
41+
<activity
42+
android:name="com.example.helloworld.AudioActivity"
43+
android:label="Audio Activity" >
44+
</activity>
4145
</application>
4246

4347
</manifest>

res/layout/activity_audio.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical" >
6+
7+
<Button
8+
android:id="@+id/buttonAudio1"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:text="Button" />
12+
13+
<Button
14+
android:id="@+id/buttonAudio2"
15+
android:layout_width="wrap_content"
16+
android:layout_height="wrap_content"
17+
android:text="Button" />
18+
19+
<Button
20+
android:id="@+id/buttonAudio3"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:text="Button" />
24+
25+
</LinearLayout>

res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
android:id="@+id/button5"
4444
android:layout_width="wrap_content"
4545
android:layout_height="wrap_content"
46-
android:text="Button" />
46+
android:text="Audio" />
4747

4848
<Button
4949
android:id="@+id/button6"

res/layout/activity_textfile.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
android:orientation="vertical" >
66

77
<EditText
8-
android:id="@+id/editText1"
8+
android:id="@+id/editText_FileExample"
99
android:layout_width="match_parent"
1010
android:layout_height="wrap_content"
1111
android:layout_weight="0.14"
@@ -16,7 +16,7 @@
1616
</EditText>
1717

1818
<Button
19-
android:id="@+id/button1"
19+
android:id="@+id/button_FileExample"
2020
android:layout_width="wrap_content"
2121
android:layout_height="wrap_content"
2222
android:layout_gravity="center"

res/raw/champion.wav

153 KB
Binary file not shown.

res/raw/eum.wav

49.4 KB
Binary file not shown.

res/raw/mburger.wav

21.6 KB
Binary file not shown.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.example.helloworld;
2+
3+
import android.app.Activity;
4+
import android.media.AudioManager;
5+
import android.media.MediaPlayer;
6+
import android.os.Bundle;
7+
import android.view.View;
8+
import android.view.View.OnClickListener;
9+
10+
public class AudioActivity extends Activity implements OnClickListener {
11+
12+
private MediaPlayer mp;
13+
14+
@Override
15+
protected void onCreate(Bundle savedInstanceState) {
16+
super.onCreate(savedInstanceState);
17+
setContentView(R.layout.activity_audio);
18+
setVolumeControlStream(AudioManager.STREAM_MUSIC);
19+
findViewById(R.id.buttonAudio1).setOnClickListener(this);
20+
findViewById(R.id.buttonAudio2).setOnClickListener(this);
21+
findViewById(R.id.buttonAudio3).setOnClickListener(this);
22+
}
23+
24+
@Override
25+
public void onClick(View v) {
26+
int id;
27+
if (v.getId() == R.id.buttonAudio1) {
28+
id = R.raw.champion;
29+
} else if (v.getId() == R.id.buttonAudio2) {
30+
id = R.raw.eum;
31+
} else { //if (v.getId() == R.id.buttonAudio3) {
32+
id = R.raw.mburger;
33+
}
34+
if(mp != null) {
35+
mp.release();
36+
}
37+
mp = MediaPlayer.create(this,id);
38+
mp.start();
39+
}
40+
41+
}

src/com/example/helloworld/MainActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class MainActivity extends Activity {
1313
private Button button1;
1414
private Button button2;
1515
private Button button3;
16+
private Button button5;
1617
private Context context;
1718

1819
@Override
@@ -26,6 +27,8 @@ protected void onCreate(Bundle savedInstanceState) {
2627
button2.setOnClickListener(new MyOnClickListener());
2728
button3 = (Button) findViewById(R.id.button3);
2829
button3.setOnClickListener(new MyOnClickListener());
30+
button5 = (Button) findViewById(R.id.button5);
31+
button5.setOnClickListener(new MyOnClickListener());
2932
}
3033

3134
@Override
@@ -45,6 +48,8 @@ public void onClick(View arg0) {
4548
startActivity(new Intent(context, PreferenceActivity.class));
4649
} else if (arg0.getId() == R.id.button3) {
4750
startActivity(new Intent(context, TextFileActivity.class));
51+
} else if (arg0.getId() == R.id.button5) {
52+
startActivity(new Intent(context, AudioActivity.class));
4853
}
4954
}
5055
}

src/com/example/helloworld/TextFileActivity.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,70 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.io.InputStreamReader;
7+
import java.io.OutputStream;
8+
import java.io.OutputStreamWriter;
9+
import java.util.Scanner;
710

811
import android.app.Activity;
12+
import android.content.Context;
913
import android.os.Bundle;
14+
import android.view.View;
15+
import android.widget.Button;
1016
import android.widget.EditText;
1117

1218
public class TextFileActivity extends Activity {
1319

1420
private EditText editor;
21+
private Button button;
22+
private Context context;
1523

1624
@Override
1725
protected void onCreate(Bundle savedInstanceState) {
1826
super.onCreate(savedInstanceState);
1927
setContentView(R.layout.activity_textfile);
20-
editor = (EditText) findViewById(R.id.editText1);
28+
editor = (EditText) findViewById(R.id.editText_FileExample);
29+
button = (Button) findViewById(R.id.button_FileExample);
30+
button.setOnClickListener(new WriteToFileListener());
2131

2232
try {
2333
// insert text file in the assets folder
24-
InputStream in = this.getAssets().open("readme.txt");
34+
// you can't write to the assests folder, it is read-only
35+
//InputStream in = this.getAssets().open("readme.txt");
36+
37+
// but you can write to the local filesystem
38+
InputStream in = openFileInput("readme.txt");
2539
BufferedReader reader = new BufferedReader(
2640
new InputStreamReader(in));
2741
String str;
2842
while ((str = reader.readLine()) != null) {
2943
editor.append(str + "\n");
3044
}
31-
3245
in.close();
46+
47+
/*
48+
* another idiom for doing the same thing
49+
Scanner scanner = new Scanner(openFileInput("readme.txt"));
50+
while(scanner.hasNextLine()) {
51+
editor.append(scanner.nextLine());
52+
}
53+
scanner.close();
54+
*/
3355
} catch (IOException e) {
3456
e.printStackTrace();
3557
}
58+
}
3659

60+
public class WriteToFileListener implements View.OnClickListener {
61+
@Override
62+
public void onClick(View v) {
63+
try {
64+
OutputStream out = openFileOutput("readme.txt", MODE_APPEND);
65+
OutputStreamWriter writer = new OutputStreamWriter(out);
66+
writer.write(editor.getText().toString());
67+
writer.close();
68+
} catch (Exception e) {
69+
70+
}
71+
}
3772
}
3873
}

0 commit comments

Comments
 (0)