Skip to content

Commit bb69192

Browse files
authoredJan 8, 2020
Create 不安全储存:Android外部存储器
1 parent 4e0e009 commit bb69192

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
 
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
写入应用程序特定的位置,例如应用程序的 SQLite 数据库。Android 完全支持 SQLite 数据库。应用程序内的任意类都可以按名称访问您所创建的任意数据库,应用程序外的类则无法访问。
2+
<b>例 2:</b>通过创建 SQLiteOpenHelper 的子类和替代 OnCreate() 方法来创建一个新的 SQLite 数据库。
3+
<pre>
4+
public class MyDbOpenHelper extends SQLiteOpenHelper {
5+
6+
private static final int DATABASE_VERSION = 2;
7+
private static final String DICTIONARY_TABLE_NAME = &quot;dictionary&quot;;
8+
private static final String DICTIONARY_TABLE_CREATE =
9+
&quot;CREATE TABLE &quot;
10+
+ DICTIONARY_TABLE_NAME + &quot;
11+
(&quot;
12+
+
13+
KEY_WORD + &quot;
14+
TEXT, &quot;
15+
+
16+
KEY_DEFINITION + &quot;
17+
TEXT);&quot;;
18+
19+
DictionaryOpenHelper(Context context) {
20+
super(context, DATABASE_NAME, null, DATABASE_VERSION);
21+
}
22+
23+
@Override
24+
public void onCreate(SQLiteDatabase db) {
25+
db.execSQL(DICTIONARY_TABLE_CREATE);
26+
}
27+
}
28+
</pre>
29+
<b>例 3:</b>或者,写入设备的内部存储。默认情况下,保存到内部存储的文件是 Android 应用程序专用的。其他
30+
应用程序和用户均无法访问它们。用户卸载该应用程序时,这些文件将随之删除。
31+
在下列代码中,我们创建了一个专用文件并将其写入内部存储。我们声明了 Context.MODE_PRIVATE。
32+
MODE_PRIVATE 会创建一个文件(或替换同名文件),并使其成为您应用程序的专用文件。
33+
<pre>
34+
String FILENAME = &quot;hello_file&quot;;
35+
String string = &quot;hello world!&quot;;
36+
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
37+
fos.write(string.getBytes());
38+
fos.close();
39+
</pre>

0 commit comments

Comments
 (0)
Please sign in to comment.