Search code examples
androidandroid-intentinternal-storageandroid-internal-storage

Share text file via Intent from internal storage


I create a bunch of text files which I created like in the code below. I saved those files in internal storage. My goal now is to display it in ListView and if user gonna click on an item it should open or send the file via intent (e.g. email).

static void createFile (Context context) {
    mTimeStamp = String.valueOf(System.currentTimeMillis()/1000);
    try {
        String fileName = "File_" + mTimeStamp + ".txt";
        mContext = context;
        mOutputStreamWriter = new 
        OutputStreamWriter(context.openFileOutput(fileName ,Context.MODE_PRIVATE));
        String path = mContext.getFilesDir().getAbsolutePath();
        Toast.makeText(mContext, "Creating file: " +  path, 
        Toast.LENGTH_LONG).show();

    } catch (FileNotFoundException e) {
        Log.e(TAG,"Error with creating file writer...");
        e.printStackTrace();
    }
}

static void writeToFileData(String dataToSave) {
    try {
        Toast.makeText(mContext, "Saving data to file: " +  dataToSave, Toast.LENGTH_SHORT).show();
        mOutputStreamWriter.write(dataToSave);
    } catch (IOException e) {
        Log.e(TAG,"Error with writing to file...");
        e.printStackTrace();
    }
}

static void closeFileWriter() {
    try {
        Toast.makeText(mContext, "Closing file... ", Toast.LENGTH_SHORT).show();
        mOutputStreamWriter.close();

    } catch (IOException e) {
        Log.e(TAG,"Error with closing file writer...");
        e.printStackTrace();
    }
}

Then I list my all files like this (not in ListView but only for test now):

static void testDispFiles (Context context) {
    mContext = context;
    String path = mContext.getFilesDir().getAbsolutePath();
    Log.v(TAG, "testDisp path: " + path);
    File dirFile = new File(path);

    File[] files = dirFile.listFiles();
    Log.v(TAG, "testDisp length: " + files.length);

    for (File f: files) {
        Log.v(TAG, "testDisp file: " + f.getName());
    }
}

And now I want to share one of the files by sending it through e.g. email. How to send the text file from internal storage via intent?

//Edit:

I create an Intent which returning the toast "You can't add this file" message from Gmail app.

String path = mContext.getFilesDir().getAbsolutePath();
File f = new File(path);
File[] files = f.listFiles();
Uri data = Uri.fromFile(files[0]);
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_STREAM,data);
i.setType("plain/text");
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivity(i);

Solution

  • I found one of solution for this problem. How @CommonsWare suggested I used a FileProvider. I follow this example. But in provider_paths.xml I put this code, because I use a internal storage.

    <Paths xmlns:android="http://schemas.android.com/apk/res/android">
        <files-path name="files" path="."/>
    </Paths>
    

    Then in activity I send a file via intent like this:

           Uri path = FileProvider.getUriForFile(this,"same_authoritory_which_was_in_manifest", new File(FILE_TO_SAVE.getPath()));
           Intent i = new Intent(Intent.ACTION_SEND);
           i.putExtra(Intent.EXTRA_TEXT, "Test");
           i.putExtra(Intent.EXTRA_STREAM, path);
           i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
           i.setType("plain/*");
           startActivity(i);