Search code examples
androidandroid-sharing

Share and receive simple data from a file


Using this help I want to recover the text inside a file, I make a long press on the file, I can choose the application to use, so I take the one I realize, the application takes into account my action, but I cannot recover the text from the file. The variable “sharedText” is null. here is the code below.

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textHello = findViewById(R.id.hello);

    Intent intent = getIntent();
    String action = intent.getAction();
    String type   = intent.getType();

    Log.d(TAG," action = "+action); // = android.intent.action.SEND
    Log.d(TAG," type =  "+type);    // = text/plain

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {

            String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);

            Log.d(TAG, " sharedText =  " + sharedText); // = null

            if (sharedText != null) {

               textHello.setText(sharedText);

            } else {
                sharedText = "it doesn't work for me";
                textHello.setText(sharedText);

            }
        }
    }
}

}

here is the manifeste

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>
</application>

Thank you for your answers.


Solution

  • Eureka, surfing a lot here is the solution to my problem and I hope it will help other people, I saw a lot of similar problems here is the link that helped me lien

    and here is the code

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        textHello = findViewById(R.id.hello);
    
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
    
        Log.d(TAG, " action = " + action); // = android.intent.action.SEND
        Log.d(TAG, " type =  " + type);    // = text/plain
    
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
    
    
    
    
                Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    
                Log.d(TAG, " uri =  " + uri);
    
                StringBuilder text = new StringBuilder();
                try {
                    InputStream inputStream = getContentResolver().openInputStream(uri);
                    BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
                    String mLine;
                    while ((mLine = r.readLine()) != null) {
    
                        Log.d(TAG, " text =  " + mLine);
    
                        text.append(mLine);
                        text.append('\n');
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                textHello.setText(text);
    
    
    
    
            }
         }
    }