简体   繁体   中英

Android open text file to read after Intent.ACTION_GET_CONTENT

The flow is:

  1. The user needs to select text file for use and the default Android explorer whatever pops up.
  2. Then I want to store string containing the file name, to actually open the file for reading.
  3. I want to open that file and rewrite him to new file on app internal storage.
  4. I want to open the new created file from app internal storage.
  5. Bonus 1 - If it's now .txt file but .doc , I want to convert him to regular .txt file in step 3 above of rewriting.
    Bonus 2 - How to handle large text files?

Here's the code:

// 1. Start with user action pressing on button to select file
addButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        startActivityForResult(intent, PICKFILE_RESULT_CODE);          
    }
});

// 2. Come back here
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICKFILE_RESULT_CODE) {
        // Get the Uri of the selected file
        Uri uri = data.getData();
        String filePathName = "WHAT TODO ?";
        LaterFunction(filePathName);
    }
}

// 3. Later here
public void LaterFunction(String filePathName) {
    BufferedReader br;
    FileOutputStream os;
    try {
        br = new BufferedReader(new FileReader("WHAT TODO ?"));
        //WHAT TODO ? Is this creates new file with 
        //the name NewFileName on internal app storage?
        os = openFileOutput("newFileName", Context.MODE_PRIVATE);                     
        String line = null;
        while ((line = br.readLine()) != null) {
            os.write(line.getBytes());
        }
        br.close();
        os.close();
        lastFunction("newFileName");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();     
    }
}

// 4. And in the end here
public void lastFunction(String newFileName) {
    //WHAT TODO? How to read line line the file 
    //now from internal app storage?
}

Step #1: Delete String filePathName = "WHAT TODO ?";

Step #2: Change LaterFunction(filePathName); to LaterFunction(uri);

Step #3: Change br = new BufferedReader(new FileReader("WHAT TODO ?")); to br = new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(uri));

That is the minimum necessary to address your question.

However, a MIME type of */* will match any type of file, not just text files. Binary files should not be copied using readLine() . If you only want plain text files, use text/plain instead of */* .

For those who struggle to fix that code here is the fixed one

ab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                startActivityForResult(intent, PICKFILE_RESULT_CODE);
            }
        });

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICKFILE_RESULT_CODE) {
            Uri uri = data.getData();
            LaterFunction(uri);
        }
    }

public void LaterFunction(Uri uri) {
        BufferedReader br;
        FileOutputStream os;
        try {
            br = new BufferedReader(new InputStreamReader(getContentResolver().openInputStream(uri)));
            //WHAT TODO ? Is this creates new file with
            //the name NewFileName on internal app storage?
            os = openFileOutput("newFileName", Context.MODE_PRIVATE);
            String line = null;
            while ((line = br.readLine()) != null) {
                os.write(line.getBytes());
                Log.w("nlllllllllllll",line);
            }
            br.close();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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