Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 76c662c

Browse files
committed
Bug fix for 4-1
1 parent 51d2e5a commit 76c662c

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed

AndroidTutorial4_1Update.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
# 錯誤修正:4-1 使用照相機與麥克風
2+
3+
這一章加入的照相與錄音功能,把照片與錄音檔案名稱儲存在同一個欄位,因此造成衝突。依照下列的步驟修正錯誤:
4+
5+
1. 開啟「Item.java」,加入下列的欄位與方法宣告:
6+
7+
// 錄音檔案名稱
8+
private String recFileName;
9+
10+
public String getRecFileName() {
11+
return recFileName;
12+
}
13+
14+
public void setRecFileName(String recFileName) {
15+
this.recFileName = recFileName;
16+
}
17+
18+
2. 同樣在「Item.java」,為建構子加入錄音檔案名稱參數:
19+
20+
// 錄音檔案名稱參數:String recFileName
21+
public Item(long id, long datetime, Colors color, String title,
22+
String content, String fileName, String recFileName,
23+
double latitude, double longitude, long lastModify) {
24+
this.id = id;
25+
this.datetime = datetime;
26+
this.color = color;
27+
this.title = title;
28+
this.content = content;
29+
this.fileName = fileName;
30+
// 錄音檔案名稱
31+
this.recFileName = recFileName;
32+
this.latitude = latitude;
33+
this.longitude = longitude;
34+
this.lastModify = lastModify;
35+
}
36+
37+
3. 開啟「ItemDAO.java」,加入與修改下列的欄位宣告:
38+
39+
...
40+
// 錄音檔案名稱
41+
public static final String RECFILENAME_COLUMN = "recfilename";
42+
...
43+
// 在「FILENAME_COLUMN」下方加入錄音檔案名稱欄位
44+
public static final String CREATE_TABLE =
45+
"CREATE TABLE " + TABLE_NAME + " (" +
46+
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
47+
DATETIME_COLUMN + " INTEGER NOT NULL, " +
48+
COLOR_COLUMN + " INTEGER NOT NULL, " +
49+
TITLE_COLUMN + " TEXT NOT NULL, " +
50+
CONTENT_COLUMN + " TEXT NOT NULL, " +
51+
FILENAME_COLUMN + " TEXT, " +
52+
RECFILENAME_COLUMN + " TEXT, " + // 增加錄音檔案名稱
53+
LATITUDE_COLUMN + " REAL, " +
54+
LONGITUDE_COLUMN + " REAL, " +
55+
LASTMODIFY_COLUMN + " INTEGER, " +
56+
ALARMDATETIME_COLUMN + " INTEGER)";
57+
58+
4. 同樣在「ItemDAO.java」,修改「insert」方法:
59+
60+
public Item insert(Item item) {
61+
ContentValues cv = new ContentValues();
62+
63+
cv.put(DATETIME_COLUMN, item.getDatetime());
64+
cv.put(COLOR_COLUMN, item.getColor().parseColor());
65+
cv.put(TITLE_COLUMN, item.getTitle());
66+
cv.put(CONTENT_COLUMN, item.getContent());
67+
cv.put(FILENAME_COLUMN, item.getFileName());
68+
// 錄音檔案名稱
69+
cv.put(RECFILENAME_COLUMN, item.getRecFileName());
70+
cv.put(LATITUDE_COLUMN, item.getLatitude());
71+
cv.put(LONGITUDE_COLUMN, item.getLongitude());
72+
cv.put(LASTMODIFY_COLUMN, item.getLastModify());
73+
cv.put(ALARMDATETIME_COLUMN, item.getAlarmDatetime());
74+
long id = db.insert(TABLE_NAME, null, cv);
75+
item.setId(id);
76+
77+
return item;
78+
}
79+
80+
5. 同樣在「ItemDAO.java」,修改「update」方法:
81+
82+
public boolean update(Item item) {
83+
ContentValues cv = new ContentValues();
84+
85+
cv.put(DATETIME_COLUMN, item.getDatetime());
86+
cv.put(COLOR_COLUMN, item.getColor().parseColor());
87+
cv.put(TITLE_COLUMN, item.getTitle());
88+
cv.put(CONTENT_COLUMN, item.getContent());
89+
cv.put(FILENAME_COLUMN, item.getFileName());
90+
// 錄音檔案名稱
91+
cv.put(RECFILENAME_COLUMN, item.getRecFileName());
92+
cv.put(LATITUDE_COLUMN, item.getLatitude());
93+
cv.put(LONGITUDE_COLUMN, item.getLongitude());
94+
cv.put(LASTMODIFY_COLUMN, item.getLastModify());
95+
cv.put(ALARMDATETIME_COLUMN, item.getAlarmDatetime());
96+
String where = KEY_ID + "=" + item.getId();
97+
98+
return db.update(TABLE_NAME, cv, where, null) > 0;
99+
}
100+
101+
6. 同樣在「ItemDAO.java」,修改「getRecord」方法:
102+
103+
public Item getRecord(Cursor cursor) {
104+
Item result = new Item();
105+
106+
result.setId(cursor.getLong(0));
107+
result.setDatetime(cursor.getLong(1));
108+
result.setColor(ItemActivity.getColors(cursor.getInt(2)));
109+
result.setTitle(cursor.getString(3));
110+
result.setContent(cursor.getString(4));
111+
result.setFileName(cursor.getString(5));
112+
// 錄音檔案名稱
113+
result.setRecFileName(cursor.getString(6));
114+
// 後續的編號都要修改
115+
result.setLatitude(cursor.getDouble(7));
116+
result.setLongitude(cursor.getDouble(8));
117+
result.setLastModify(cursor.getLong(9));
118+
result.setAlarmDatetime(cursor.getLong(9));
119+
120+
return result;
121+
}
122+
123+
7. 同樣在「ItemDAO.java」,修改「sample」方法:
124+
125+
public void sample() {
126+
// 增加錄音檔案名稱參數「""」
127+
Item item = new Item(0, new Date().getTime(), Colors.RED, "關於Android Tutorial的事情.", "Hello content", "", "", 0, 0, 0);
128+
Item item2 = new Item(0, new Date().getTime(), Colors.BLUE, "一隻非常可愛的小狗狗!", "她的名字叫「大熱狗」,又叫\n作「奶嘴」,是一隻非常可愛\n的小狗。", "", "", 25.04719, 121.516981, 0);
129+
Item item3 = new Item(0, new Date().getTime(), Colors.GREEN, "一首非常好聽的音樂!", "Hello content", "", "", 0, 0, 0);
130+
Item item4 = new Item(0, new Date().getTime(), Colors.ORANGE, "儲存在資料庫的資料", "Hello content", "", "", 0, 0, 0);
131+
132+
insert(item);
133+
insert(item2);
134+
insert(item3);
135+
insert(item4);
136+
}
137+
138+
8. 開啟「MyDBHelper.java」,增加資料庫版本編號:
139+
140+
// 資料庫版本,資料結構改變的時候要更改這個數字,通常是加一
141+
public static final int VERSION = 3;
142+
143+
9. 開啟「ItemActivity.java」,增加錄音檔案名稱欄位變數:
144+
145+
// 錄音檔案名稱
146+
private String recFileName;
147+
148+
10. 同樣在「ItemActivity.java」,增加取得錄音檔案名稱的方法:
149+
150+
private File configRecFileName(String prefix, String extension) {
151+
// 如果記事資料已經有檔案名稱
152+
if (item.getRecFileName() != null && item.getRecFileName().length() > 0) {
153+
recFileName = item.getRecFileName();
154+
}
155+
// 產生檔案名稱
156+
else {
157+
recFileName = FileUtil.getUniqueFileName();
158+
}
159+
160+
return new File(FileUtil.getExternalStorageDir(FileUtil.APP_DIR),
161+
prefix + recFileName + extension);
162+
}
163+
164+
11. 同樣在「ItemActivity.java」,修改啟動錄音元件的方法:
165+
166+
public void clickFunction(View view) {
167+
int id = view.getId();
168+
169+
switch (id) {
170+
...
171+
case R.id.record_sound:
172+
// 修改呼叫方法的名稱為「configRecFileName」
173+
final File recordFile = configRecFileName("R", ".mp3");
174+
175+
if (recordFile.exists()) {
176+
...
177+
}
178+
// 如果沒有錄音檔,啟動錄音元件
179+
else {
180+
goToRecord(recordFile);
181+
}
182+
183+
break;
184+
...
185+
}
186+
187+
}
188+
189+
完成全部的修改以後執行應用程式,測試同一個記事資料照相與錄音的功能。

0 commit comments

Comments
 (0)