FileNotFoundException: /mimetype/mnt/sdcard/file.csv 的异常

我想读取SD卡中的csv文件,但是当我选择文件时,有一个文件不能被找到的异常。

代码如下:

Intent intent = new Intent();             
    intent.setType("file/csv");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select csv"),
            SELECT_CSV_Dialog);

 if (resultCode == RESULT_OK) {
 if (requestCode == 1) {
  data = result.getData();//data is the URI
 System.out.println("res "+data);
 if (data.getLastPathSegment().endsWith("csv") || data.getLastPathSegment().endsWith("CSV")) {       
     try {
         File f = new File(data.getPath());//this is where i get the file not found             
         FileInputStream  fis =new FileInputStream(f);   
            fis = this.openFileInput(data.toString());
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(fis));
                             String line;
            while ((line = reader.readLine()) != null) {
                String[] RowData = line.split(",");
                System.out.println("row  "+RowData.length);
                if(RowData.length==2){                      

        Toast.makeText(Importt.this, "Schema Supported", Toast.LENGTH_SHORT).show();
                    break;
                }else{
    Toast.makeText(Importt.this, "Schema not Supported", Toast.LENGTH_SHORT).show();
                }
            }}

我从这里获取的错误:"File f = new File(data.getPath());"
为什么会出现这个异常呢?

使用下面的代码从csv 文件中读取数据。

try
   {
    List<String> list = new ArrayList<String>();
    String line;

    String fileName = "/mnt/sdcard/test.csv";
    FileReader reader = new FileReader(fileName);
    BufferedReader bufrdr = new BufferedReader(reader);
    line = bufrdr.readLine();
    while (line != null) {
        list.add(line);
        line = bufrdr.readLine();
    }
    bufrdr.close();
    reader.close();

    String[] array = new String[list.size()];
    list.toArray(array);

    for (int i = 0; i < list.size(); i++) {
        System.out.println(" 22222222 0 0 " + list.get(i).toString() );
    }

 }
  catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
}

对于 SD-CARD 使用下面的代码检查:

static public boolean hasStorage(boolean requireWriteAccess) {
    //TODO: After fix the bug,  add "if (VERBOSE)" before logging errors.
    String state = Environment.getExternalStorageState();
    Log.v(TAG, "storage state is " + state);

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        if (requireWriteAccess) {
            boolean writable = checkFsWritable();
            Log.v(TAG, "storage writable is " + writable);
            return writable;
        } else {
            return true;
        }
    } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}



body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;} </style> <p></head><br> <body></p> <pre><code>&lt;div id=&quot;div_map&quot; style=&quot;width:100%;height:100%;&quot;&gt;&lt;/div&gt; </code></pre> <script type="text/javascript"> var map=null; map=new STMapObj("div_map"); map.locateMap(new STMapPoint(121.778885,31.049919),5); map.setZoomCompVisible(false); map.setScaleCompVisible(false); geocode(); //地理编码 function geocode() { map.geoCoding("上海市钦州南路81号",geofunc); } function geofunc(x,y) { alert(x+","+y) }
var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://"); document.write(unescape("%3Cspan id='cnzz_stat_icon_1000501913'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s11.cnzz.com/z_stat.php%3Fid%3D1000501913' type='text/javascript'%3E%3C/script%3E"));


逻辑没有问题,我也这么写过。可能是你做判断的时候data.getLastPathSegment()函数处理有问题,我没这么写

FileNotFoundException: /mimetype/mnt/sdcard/file.csv 明显不对,文件系统一般都没有/mimetype这个目录,根目录应该是/mnt,所以是你的data数据没处理好的问题!

看看Manifest.xml中是否设置了

body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}


<div id="div_map" style="width:100%;height:100%;"></div> 
var map=null; map=new STMapObj(&quot;div_map&quot;); map.locateMap(new STMapPoint(121.778885,31.049919),5); map.setZoomCompVisible(false); map.setScaleCompVisible(false); geocode(); //地理编码 function geocode() { map.geoCoding(&quot;上海市钦州南路81号&quot;,geofunc); } function geofunc(x,y) { alert(x+&quot;,&quot;+y) } <br> var cnzz_protocol = ((&quot;https:&quot; == document.location.protocol) ? &quot; https://&quot; : &quot; http://&quot;); document.write(unescape(&quot;%3Cspan id=&#39;cnzz_stat_icon_1000501913&#39;%3E%3C/span%3E%3Cscript src=&#39;&quot; + cnzz_protocol + &quot;s11.cnzz.com/z_stat.php%3Fid%3D1000501913&#39; type=&#39;text/javascript&#39;%3E%3C/script%3E&quot;)); </p>

有异常的话你写的时候要抛出异常阿

检查路径是否正确, 是否可以读取