MediaScannerConnection doesn't work

狂风中的少年 提交于 2021-01-29 12:47:02

问题


public void onClick(View v) {
    // Writing data to file
    FileWriter fw;
    try {
        fw = new FileWriter(Environment.getExternalStorageDirectory()+"/DataLog.csv", true);

        BufferedWriter br = new BufferedWriter(fw);
        br.append(formattedDate + String.valueOf(location.getLatitude()) + 
                ";" + String.valueOf(location.getLongitude()) + 
                ";" + String.valueOf(location.getSpeed()) + 
                ";" + String.valueOf(location.getBearing()) + 
                ";" + String.valueOf(location.getAltitude()) + 
                ";" + String.valueOf(location.getAccuracy()));
        br.append("\r\n");
        br.close();
        fw.close();

        // MediaScanner scans the file

        MediaScannerConnection.scanFile(MainActivity.this, new String[] {fw.toString()} , null, new MediaScannerConnection.OnScanCompletedListener() {

                @Override
                public void onScanCompleted(String path, Uri uri) {
                Toast t = Toast.makeText(MainActivity.this, "Scan comlete", Toast.LENGTH_LONG);
                t.show();
                }
                } );
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I tried a code to write data to a DataLog.csv file in the sd root. The code creates the file with the data but i cannot see the file in windows when browsing the sdcard. I saw this video and followed the instructions but it is not working for me. Maybe the fw variable is not good to define the file?

File csv = new File (Environment.getExternalStorageDirectory(), "DataLog.csv");

                    MediaScannerConnection.scanFile(
                            MainActivity.this,
                           new String[] {csv.getAbsolutePath()},
                            null, null);

I tried your advice like this but it still doing nothing.


回答1:


toString() on FileWriter does not return the path to the file, which you are assuming it does, in the second parameter you pass to scanFile().



来源:https://stackoverflow.com/questions/17971263/mediascannerconnection-doesnt-work

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