简体   繁体   中英

Trying to read Text File from Uri Android

I created an android file chooser that returns the uri of a text file. I want to open and read the file and store it's data.My code is:

private void covertFile(Uri data) {

        InputStream inputStream = getContentResolver().openInputStream(data);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        String myText = "";
        int in;
        try {
            in = inputStream.read();
            while (in != -1)
            {
                byteArrayOutputStream.write(in);
                in = inputStream.read();
            }
            inputStream.close();

            myText = byteArrayOutputStream.toString();
        }catch (IOException e) {
            e.printStackTrace();
        }

        myTextView.setText(myText);
    }

But the line InputStream inputStream = getContentResolver().openInputStream(data); gives java.Io.FileNotFoundException . How do I resolve this?

EDIT: Issue Resolved

First create file object

    String path = data.toString();
    File file = new File(path);

Now pass the file object as arg in InputStream

    InputStream inputStream = getContentResolver().openInputStream(file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM