fregomene fregomene - 16 days ago 7
Android Question

How to get String from a .txt file in android

I'm writing a code which creates a file chooser for only .txt files and then represents its contents in a String. The problem is that after selecting a file nothing happens (log says that result code is -1, and request code is 0). Can anyone help me? Here is my chooser for .txt files:

public void onUploadClicked(View view) {
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/plain");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, getText(R.string.select_file)), REQUEST_CODE);

}


Here is my OnActivityResult method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == Activity.RESULT_OK) {
if (data == null) {
return;
} else {
Uri uri = data.getData();
uploadedFile = new File(uri.getPath());
try {
readFile();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
return;
}
}


And here is my method for reading .txt file into a String:

private void readFile() throws IOException {

uploadedString = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(uploadedFile));
String line;
while ((line = reader.readLine()) != null) {

uploadedString.append(line);
uploadedString.append('\n');
}
Log.i("Uploaded successfully: ", uploadedString.toString());
reader.close();
}

Answer

Try this

public static int PICK_FILE = 1;

Then overriding onActivityResult()

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_FILE) {
            if (resultCode == RESULT_OK) {
                // User pick the file
                Uri uri = data.getData();
                String fileContent = readTextFile(uri);
                Toast.makeText(this, fileContent, Toast.LENGTH_LONG).show();
            } else {
                Log.i(TAG, data.toString());
            }
        }
    }

Method to read the text file picked by user

    private String readTextFile(Uri uri){
        BufferedReader reader = null;
        StringBuilder builder = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(uri)));
            String line = "";

            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return builder.toString();
    }

Create an implicit intent

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/plain");
startActivityForResult(intent, PICK_FILE);
Comments