Android - 媒体扫描程序问题

时间:2014-01-23 21:01:35

标签: java android sqlite

我正在研究将SQLite数据库表导出到特定目录中的csv文件的方法。我已经能够解决所有问题,除非我在将手机插入计算机时无法找到文件。我可以在手机上看到文件夹和文件,而不是在我的电脑上。我使用MediaScannerConnection.scanfile方法,但仍然无法让我的电脑找到文件夹和文件。我添加了一个OnCompleted侦听器,它在logcat中返回正确的目录路径,但我仍然无法在我的计算机上看到它。我知道重新启动会强制进行彻底的重新扫描,但我并不是我的应用程序的用户每次都必须这样做。我将在下面发布源代码。在此先感谢您的帮助。

GameView.java:

case R.id.exportCSV:
            gameView.open();
            boolean export = gameView.exportTable(game);
            gameView.close();
            if(export = true){
                Toast t = Toast.makeText(getApplicationContext(), "CSV file saved to /Statbook CSV Files Folder", Toast.LENGTH_SHORT);
                t.show();
                MediaScannerConnection.scanFile(GameView.this, new String[] {Environment.getExternalStorageDirectory().toString()+"/Statbook CSV Files"}, null, new MediaScannerConnection.OnScanCompletedListener() {

                    @Override
                    public void onScanCompleted(String arg0, Uri arg1) {
                        // TODO Auto-generated method stub
                        Log.i("External Storage", "Scanned " + arg0 + ":" + "-> uri=" + arg1);
                    }
                });

exportTable方法:

public boolean exportTable(String fileName) {
        // TODO Auto-generated method stub
        boolean result = true;
        String csvHeaders = "id, Player Number, Player Name, 2 Pointers Made, 2 Pointers Missed, Total 2 Pointers," +
                "3 Pointers Made, 3 Pointers Missed, Total 3 Points, Free Throws Made, Free Throws Missed," +
                "Total Free Throws, Offensive Rebounds, Defensive Rebounds, Assists, Fouls, Turnovers," +
                "Blocks, Steals";
        String csvValues = "";

        try {
            File myDir = new File(Environment.getExternalStorageDirectory()+"/Statbook CSV Files");
            if(!myDir.exists()){
                myDir.mkdir();
            }
            File outFile = new File(Environment.getExternalStorageDirectory()+"/Statbook CSV Files",fileName+".csv");
            FileWriter writer = new FileWriter(outFile);
            BufferedWriter out = new BufferedWriter(writer);
            Cursor C = ourDatabase.query(fileName, null,null, null, null, null, null);
            if (C != null) {
                out.write(csvHeaders);
                out.newLine();
                C.moveToFirst();
                while(!C.isAfterLast()){
                    for (int i = 0; i < C.getColumnCount(); i++) {
                        csvValues += C.getString(i) + ",";
                    }
                    out.write(csvValues);
                    out.newLine();
                    csvValues = "";
                    C.moveToNext();
                }
                out.close();
                result = true;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            result = false;
            e.printStackTrace();
        }
        return result;
    }

1 个答案:

答案 0 :(得分:0)

我找到了问题的原因。在MediaScannerConnection.scanfile中,我只传递了存储文件的目录的路径,而不是文件本身的路径。将一点点添加到路径的末尾后,一切正常。

更新了MediaScannerConnection:

MediaScannerConnection.scanFile(GameView.this, new String[] {Environment.getExternalStorageDirectory().toString()+"/Statbook CSV Files/"+game+".csv"}, null, new MediaScannerConnection.OnScanCompletedListener() {

                    @Override
                    public void onScanCompleted(String arg0, Uri arg1) {
                        // TODO Auto-generated method stub
                        Log.i("External Storage", "Scanned " + arg0 + ":" + "-> uri=" + arg1);
                }