Environment.getExternalStorageDirectory().getAbsolutePath() not working and giving /storage

雨燕双飞 提交于 2019-12-22 10:44:19

问题


My code

myDb = openOrCreateDatabase("/sdcard/FashionGirl/ImagesDB.db", Context.MODE_PRIVATE, null);
myDb = openOrCreateDatabase(dbPath, Context.MODE_PRIVATE, null);

worked perfectly, but was giving warning Do not hardcode "/sdcard/"; use Environment.getExternalStorageDirectory().getPath() instead

So I tried,

String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "FashionGirl/ImagesDB.db";
myDb = openOrCreateDatabase(dbPath, Context.MODE_PRIVATE, null);

But strangely, its not working, where Environment.getExternalStorageDirectory().getAbsolutePath() has value /storage

So its giving error,

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)

So why something recommended by Android is not working, what should I do?


回答1:


String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "FashionGirl/ImagesDB.db";
myDb = openOrCreateDatabase(dbPath, Context.MODE_PRIVATE, null);

Please do not use concatenation for constructing file paths. Try:

File dbPath = new File(Environment.getExternalStorageDirectory(), "FashionGirl/ImagesDB.db");
myDb = openOrCreateDatabase(dbPath.getAbsolutePath(), Context.MODE_PRIVATE, null);

But strangely, its not working, where Environment.getExternalStorageDirectory().getAbsolutePath() has value /storage

That is because /sdcard has been deprecated for nearly three years.




回答2:


Add / before Fashiongirl

String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/FashionGirl/ImagesDB.db";



回答3:


@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


来源:https://stackoverflow.com/questions/13915690/environment-getexternalstoragedirectory-getabsolutepath-not-working-and-givi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!