Wednesday, August 30, 2017

CSV input from cloud or external drive in Android Application



Needs to read external storage permission on file: AndroidMainiest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Now our desired intent "performFileSearch" mehtod call

/**
 * Fires an intent to spin up the "file chooser" UI and select an CSV.
 */
private static final int READ_REQUEST_CODE = 42;

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 CSV, using the CSV MIME data type.
    // If one wanted to search for ogg vorbis files, the type would be "image/*".
    // To search for all documents available via installed storage providers,
    // it would be "*/*".


    intent.setType("text/csv"); 
    startActivityForResult(intent, READ_REQUEST_CODE);
}
 
Note the following:
  • When the app fires the ACTION_OPEN_DOCUMENT intent, it launches a picker that displays all matching document providers.
  • Adding the category CATEGORY_OPENABLE to the intent filters the results to display only documents that can be opened, such as CSV files.
  • The statement intent.setType("text/csv") further filters to display only documents that have the CSV MIME data type.

Process Results

After the user selects a document in the picker, onActivityResult() gets called. The resultData parameter contains the URI that points to the selected document. Extract the URI using getData(). When you have it, you can use it to retrieve the document the user wants. For example:

@Override
public void onActivityResult(int requestCode, int resultCode,
        Intent resultData) {

    // 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 (resultData != null) {
            uri = resultData.getData();
            Log.i(TAG, "Uri: " + uri.toString());
            readCSVFile(uri);
        }
    }
}



    private void readtFile(Uri uri){

        InputStream inputStream = null;

        try {
            inputStream = getContentResolver().openInputStream(uri);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inputStream));
            String line;
            String resultString = null;

            String resultedString = null;
            Log.i("","open text file - content"+"\n");
            while ((line = reader.readLine()) != null) {

                if (resultString == null){
                    resultString = line+"\n";
                }else{
                    resultString = resultString + line+"\n";

                } 
                Log.d("csv file", "CSV File" + line+"\n");            } 
             Log.d("csv file", "Final CSV File" + resultString);
            reader.close();
            inputStream.close();
        } catch (Exception e) {
//            Log.i("csv file", "CSV File" + "Read problem");            e.printStackTrace();
        }
    }
 
 
 


 

No comments:

Post a Comment