简体   繁体   中英

Android - Media Scanner Problems

I am working on a method to export an SQLite database table to a csv file in a specific directory. I've been able to get everything sorted out, except I'm having trouble finding the file when I plug my phone into my computer. I can see the folder and file on my phone, just not on my computer. I'm using the MediaScannerConnection.scanfile method, but still can't get my computer to locate the folder and file. I added an OnCompleted listener, and it returns the correct directory path in the logcat, but I am still unable to see it on my computer. I know that rebooting will force a complete rescan, but I don't my the users my of app to have to do that every time. I'll post the source code below. Thanks in advance for your help.

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 method:

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;
    }

I found the cause of the problem. In the MediaScannerConnection.scanfile I was only passing in the path to the directory where the files were stored instead of the path to the file itself. After adding that little bit to the end of the path, everything worked fine.

Updated 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);
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM