ssummeraw 2013-07-30 05:41 采纳率: 10%
浏览 22277
已采纳

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

  • 写回答

8条回答

  • cheli_71 2013-07-30 08: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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题