从数据库

时间:2017-08-01 13:23:42

标签: android database sqlite filenames

我正在尝试获取列表中文件的名称,问题是我通过路径获取它们,例如“storage / emulated / 0 / recording-01-08-2017 04-19-23 PM”。 3GP”。 所以我使用了.replaceAll(“[^ \ d-]”,“”);并且我仍然在开头得到“0”并且它看起来像,“0 01-08-2017 04-19-23 3”

如果我想获得文件名(dateTime),我该怎么办?

//获取命名文件的日期

    String dateTime = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss aa", Locale.getDefault()).format(new Date());
    outPutFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording -" + dateTime + ".3gp";

    final String DIR_DATABASE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording";
    String sqliteQuery= "CREATE  TABLE  IF NOT EXISTS Recordings (ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , fileName VARCHAR)";
    SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "db.sqlite", null);
    database.execSQL(sqliteQuery);

//获取文件名

    Cursor cursor = database.rawQuery("SELECT fileName FROM Recordings", null);
    ArrayList<String> fileNames = new ArrayList<>();
    while (cursor.moveToNext()) {

        String fileName = cursor.getString(0).replaceAll("[^\\d-]", " ");
        fileNames.add(fileName);
    }
    cursor.close();
    database.close();

//列表和列表视图

    listRecord.addAll(fileNames);
    listAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, listRecord);
    recordList.setAdapter(listAdapter);

0 个答案:

没有答案