【问题标题】:how to read a csv file from sd card or specified path in android [duplicate]如何从sd卡或android中的指定路径读取csv文件[重复]
【发布时间】:2011-10-07 09:10:26
【问题描述】:

可能重复:
reading a specific file from sdcard in android

我正在尝试制作一个简单的 android 应用程序,该应用程序基本上会导入一个 csv 并将其插入到我的数据库表中。到目前为止,我能够读取 res 文件夹中的 csv 文件。

我的示例 csv 文件名为“test.csv”,基本上是通过“InputStream is = this.getResources().openRawResource(R.drawable.test);”访问的。

这是我的示例代码:

InputStream is = this.getResources().openRawResource
            (R.drawable.test);
                BufferedReader reader = new BufferedReader(new InputStreamReader
            (is));
                try {
                    String line;
                    String brand = "";
                    String model = "";
                    String type = "";

                    this.dh = new DataHelper(this);
                    //this.dh.deleteAllCar();
                    while ((line = reader.readLine()) != null) {
                        // do something with "line"

                        String[] RowData = line.split(",");
                        brand = RowData[1];
                        model = RowData[2];
                        type = RowData[3];
                        this.dh = new DataHelper(this);
                        //this.dh.deleteAllCar();
                        this.dh.insertcsv(brand, model, type);
                    }
                }catch (IOException ex) {
                    // handle exception
                }finally {
                    try {
                        is.close();
                    }
                    catch (IOException e) {
                        // handle exception
                    }
                }

这很好用,但是,我希望能够创建一个功能,用户可以指定从哪里获取文件(例如从手机的 sdcard 等)。但现在,我想知道如何从 sdcard(mnt/sdcard/test.csv) 访问 csv。

我们将不胜感激!谢谢,编码愉快!

【问题讨论】:

    标签: java android csv import


    【解决方案1】:

    这是关于如何写入到 SD 卡的代码,您应该能够使用上面的代码找出读取的部分:

    private void writeToSDCard() {
    try {
        File root = Environment.getExternalStorageDirectory();
    
        if (root.canWrite()){
            InputStream from = myContext.getResources().openRawResource(rID);
            File dir = new java.io.File (root, "pdf");
            dir.mkdir();
            File writeTo = new File(root, "pdf/" + attachmentName);
            FileOutputStream  to = new FileOutputStream(writeTo);
    
            byte[] buffer = new byte[4096];
            int bytesRead;
    
            while ((bytesRead = from.read(buffer)) != -1)
                to.write(buffer, 0, bytesRead); // write
            to.close();             
            from.close();
        } else {
            Log.d(TAG, "Unable to access SD card.");
        }
    } catch (Exception e) {
        Log.d(TAG, "writeToSDCard: " + e.getMessage());
    }
    }       
    }
    

    【讨论】:

    • 还是想不通,还是谢谢
    • 我已经拿到了,谢谢你的支持,我会把更新后的代码贴出来供可能需要的人参考。
    【解决方案2】:

    从 SDCard 读取文件之前已在 Stack Overflow 上介绍过。

    这是链接:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      相关资源
      最近更新 更多