Search code examples
androidandroid-intentsharedata-import

Transfer file (.txt) between apps


An application is exporting a text (.txt) file when shared through it, I can see apps like Gmail, Dropbox, drive, share it etc to share it through that app. I want to import that text file in my application for further modification of that. txt file. What should I implement in manifest or main activity? Should I use INTENT filter or should I use getUriForFile() method to receive the text file? How will my app appear in other apps sharing list which export text files?. export form whatsappsend chat via...share text file from file explorerwhatsapp extract my app is visible


Solution

  • You can use the following intent-filter for importing any file from another application into your manifest

                <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="*/*"/>
                <data android:mimeType="text/plain"/>
    

    Then use below code in your activity for getting URI for a text file

        private Uri getUriForFile() {
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        if (TextUtils.equals(Intent.ACTION_SEND, action) && !TextUtils.isEmpty(type)) {
    
            Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
            if (uri != null) {
                Log.e("uri",uri.toString());
                return  uri;
            }
        }
        return null;
    }
    

    UPDATE

    if the activity is your launcher activity then use the following intent filter

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
             <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="*/*"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>