程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-FileNotFoundException: /mimetype/mnt/sdcard/file.csv 的異常

android-FileNotFoundException: /mimetype/mnt/sdcard/file.csv 的異常

編輯:編程綜合問答
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;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved