2 ssummeraw ssummeraw 于 2013.07.30 13:41 提问

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());"
为什么会出现这个异常呢?

11个回答

cheli_71
cheli_71   2013.07.30 16:34
已采纳

使用下面的代码从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;
}
u011271055
u011271055   2014.11.24 18:46

遇到找不到的问题首先检查你的路径是不是真的正确,再做下一步判断。
你的代码里面好多都没有判断是否存在就直接进行下一步,很容易异常。

u023446369
u023446369   2014.11.19 09:29



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"));


nightcurtis
nightcurtis   2014.11.19 14:54

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

nightcurtis
nightcurtis   2014.11.19 14:54

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

cornivylove
cornivylove   2014.11.21 17:21

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

zeyanhu
zeyanhu   2014.11.27 15:55

看看Manifest.xml中是否设置了

a5628622
a5628622   2014.12.27 14:58

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>
Xuhw1991
Xuhw1991   2015.01.03 14:46

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

Xuhw1991
Xuhw1991   2015.01.03 14:46

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

共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片