1

Possible Duplicate:
reading a specific file from sdcard in android

I'm trying to make a simple android app that basically imports a csv and inserts it to my database table. So far, I was able to read a csv file inside the res folder.

my sample csv file is named "test.csv" and is basically accessed through "InputStream is = this.getResources().openRawResource(R.drawable.test);".

Here's my sample code:

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
                    }
                }

This works fine however, I want to be able to make a feature wherein the user can specify where to get the file(like from phone's sdcard, etc). But for now, I wanted to know how to access the csv from sdcard(mnt/sdcard/test.csv).

Help will be highly appreciated! thanks and happy coding!

Community
  • 1
  • 1
Rick
  • 25
  • 1
  • 3

2 Answers2

3

Reading a file from an SDCard has been covered on Stack Overflow previously.

Here's the link:

Community
  • 1
  • 1
Justin Shield
  • 2,390
  • 16
  • 12
1

Here is code on how to write to the SD Card, you should be able to figure out the read part using your code above:

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());
}
}       
}
nickfox
  • 2,835
  • 1
  • 26
  • 31