【问题标题】:Environment.getExternalStorageDirectory().getAbsolutePath() not working and giving /storageEnvironment.getExternalStorageDirectory().getAbsolutePath() 不工作并提供 /storage
【发布时间】:2012-12-04 15:20:27
【问题描述】:

我的代码

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推荐的东西不起作用,我该怎么办?

【问题讨论】:

  • "/storageFashionGirl/" 也许你需要像这样添加一个“/”: String dbPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "FashionGirl/ImagesDB.db";

标签: android path storage external android-sdcard


【解决方案1】:
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() 具有值 /storage

那是因为/sdcard 已被弃用近三年。

【讨论】:

  • @WhyandHow:这在 Java 中被认为是糟糕的形式 10 到 15 年。使用 File 构造函数不仅可以保护您免受丢​​失路径分隔符错误的影响,而且还可以处理其他平台细节(例如,/` on Windows). That latter feature is perhaps not as important for an Android-only app, but using the File` 构造函数而不是串联是一个好习惯一般。
【解决方案2】:

在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多