load . txt from internal storage in an array

Asked

Viewed 33 times

0

I’m making a stock count app and it needs to import a txt and export the same with the quantities read in the count, the problem is that searching found the library android studio to load the file from storage:

private static final int READ_REQUEST_CODE = 42;
    /**
     * Fires an intent to spin up the "file chooser" UI and select an image.
     */
    public void performFileSearch() {

        // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
        // browser.
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

        // Filter to only show results that can be "opened", such as a
        // file (as opposed to a list of contacts or timezones)
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        // Filter to show only images, using the image MIME data type.
        // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
        // To search for all documents available via installed storage providers,
        // it would be "*/*".
        intent.setType("text/plain");

        startActivityForResult(intent, READ_REQUEST_CODE);

    }

in onActivityResult this way

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null){
        String barcode = result.getContents();
        if (barcode != null && !"".equals(barcode)){
            createFragment.setBarCode(barcode);
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
    // The ACTION_OPEN_DOCUMENT intent was sent with the request code
    // READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
    // response to some other intent, and the code below shouldn't run at all.

    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        // The document selected by the user won't be returned in the intent.
        // Instead, a URI to that document will be contained in the return intent
        // provided to this method as a parameter.
        // Pull that URI using resultData.getData().
        Uri uri = null;
        if (data != null) {
            uri = data.getData();
            Log.i("resultado", "Uri: " + uri.toString());

            File file= new File(uri.getPath());
            String conteudoArquivo = file.getName();
            salvarArquivo(conteudoArquivo);
        }
    }
}

I am trying to load the txt file in an array that is loaded on the home screen, I have tried it in several ways and could not. By clicking the button it opens the storage and I can search and load the txt in the variableUri after that I cannot read the file and load in the Array

  • Where is txt stored? I recently created a library that handles incoming Uri via result. Handlepathoz, if you are interested in using it. The output of the methods I created return the actual file path string, with it it is possible to access the desired file. Inside the repository there are also examples of using the library.

1 answer

1

It is not possible to take the contents of the file directly from the File object, so use the useful java.nio.file.Files class

String conteudoArquivo = Files.readString(uri.getPath());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.