开发者

reading a specific file from sdcard in android

how to read a 开发者_开发问答specific file from sdcard. i have pushed the file in sdcard through DDMS and i am trying to read it though this way but this give me exception. can anybody tell me how to point exactly on that file?

my code is this.

String path = Environment.getExternalStorageDirectory().getAbsolutePath();
FileInputStream iStream =  new FileInputStream(path);


You are trying to read a directory... what you need is the file! Do something like this... then, you can read the file as you want.

File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");


To read any file(CSV in my case) from External Storage, we need a path for it,once you have path you can do like this...

void readFileData(String path) throws FileNotFoundException 
    {

        String[] data;
        File file = new File(path);
        if (file.exists())
        {
            BufferedReader br = new BufferedReader(new FileReader(file));
            try
            {
                String csvLine;
                while ((csvLine = br.readLine()) != null)
                {
                    data=csvLine.split(",");
                    try
                    {
                        Toast.makeText(getApplicationContext(),data[0]+" "+data[1],Toast.LENGTH_SHORT).show();
                    }
                    catch (Exception e)
                    {
                        Log.e("Problem",e.toString());
                    }
                }
            }
            catch (IOException ex)
            {
                throw new RuntimeException("Error in reading CSV file: "+ex);
            }
        }
        else
        {
            Toast.makeText(getApplicationContext(),"file not exists",Toast.LENGTH_SHORT).show();
        }
    }

/*
csv file data

17IT1,GOOGLE
17IT2,AMAZON
17IT3,FACEBOOK*/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜