我的代码
myDb = openOrCreateDatabase("/sdcard/FashionGirl/ImagesDB.db", Context.MODE_PRIVATE, null);
myDb = openOrCreateDatabase(dbPath, Context.MODE_PRIVATE, null);
工作得很好,但却发出警告Do not hardcode "/sdcard/"; use Environment.getExternalStorageDirectory().getPath() instead
所以我试过了,
String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "FashionGirl/ImagesDB.db";
myDb = openOrCreateDatabase(dbPath, Context.MODE_PRIVATE, null);
但奇怪的是,它不起作用,其中Environment.getExternalStorageDirectory()。getAbsolutePath()的值为/storage
所以给出错误,
12-17 19:32:02.230: E/SqliteDatabaseCpp(15620): sqlite3_open_v2("/storageFashionGirl/ImagesDB.db", &handle, 6, NULL) failed
12-17 19:32:02.230: E/SQLiteDatabase(15620): Failed to open the database. closing it.
12-17 19:32:02.230: E/SQLiteDatabase(15620): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file
12-17 19:32:02.230: E/SQLiteDatabase(15620): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
12-17 19:32:02.230: E/SQLiteDatabase(15620): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
12-17 19:32:02.230: E/SQLiteDatabase(15620): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
12-17 19:32:02.230: E/SQLiteDatabase(15620): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:932)
为什么Android推荐的东西不起作用,我该怎么办?
答案 0 :(得分:5)
String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "FashionGirl/ImagesDB.db";
myDb = openOrCreateDatabase(dbPath, Context.MODE_PRIVATE, null);
请不要使用连接来构建文件路径。尝试:
File dbPath = new File(Environment.getExternalStorageDirectory(), "FashionGirl/ImagesDB.db");
myDb = openOrCreateDatabase(dbPath.getAbsolutePath(), Context.MODE_PRIVATE, null);
但奇怪的是,它不起作用,其中Environment.getExternalStorageDirectory()。getAbsolutePath()具有值/存储
这是因为/sdcard
已被弃用近三年。
答案 1 :(得分:2)
在Fashiongirl
之前添加 /String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/FashionGirl/ImagesDB.db";
答案 2 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.list);
List<Employee> employees = null;
try {
XMLPullParserHandler parser = new XMLPullParserHandler();
File f = new File("/mnt/extsd/kmls/mykml.kml");
InputStream is = new FileInputStream(f);
employees = parser.parse(is);
ArrayAdapter<Employee> adapter = new ArrayAdapter<Employee>
this,R.layout.list_item, employees);
listView.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
}
Note : "/mnt/extsd/kmls/mykml.kml" this is the complete path to reach my kml fiel.
for this first you need to open your sdcard in your device then you find the path
then the path need to insert into your app.
thanks