Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving data from other application comes null in android

I want to make my application like Instagram i can share pictures directly from my application , like if i select any image from gallery and when click on share my application shows on the list of chooser

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


<activity
                android:name=".ui.activities.SplashActivity"
                android:label="@string/app_name"
                android:launchMode="singleTop"
                android:screenOrientation="sensorPortrait"
                android:windowSoftInputMode="adjustResize" >
                <intent-filter>
                    <action android:name="android.intent.action.SEND" />

                    <category android:name="android.intent.category.DEFAULT" />

                    <data android:mimeType="*/*" />
                </intent-filter>

                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
    </activity>

Code for receiving data from intent is

private void initActivityState() {
        if (getIntent() != null){
            runShareActivityIntent();
        }
}
public void runShareActivityIntent(){
        Intent passIntent=getIntent();
        String action = passIntent.getAction();
        String type = passIntent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            Uri imageUri = (Uri) passIntent.getParcelableExtra(Intent.EXTRA_STREAM);    
            Intent intentcall = new Intent(this, MainActivity.class);
            intentcall.putExtra(KEY_TYPE,type);
            intentcall.putExtra(KEY_VALUE,imageUri.toString());
            startActivity(intentcall);
            finish();
        }

    }

Now in my case when my app is in background i am able to get

String type = passIntent.getType();

and i am able to get image uri but when my app is not running( kill app ) and i select my application from chooser i get type as null

like image 309
Raj Avatar asked Jul 15 '15 05:07

Raj


People also ask

How will you share data between different applications on a device?

Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type. The system automatically identifies the compatible activities that can receive the data and displays them to the user.

How can two different android applications interact?

Using intents even allows your app to start an activity that is contained in a separate app. An Intent can be explicit in order to start a specific component (a specific Activity instance) or implicit in order to start any component that can handle the intended action (such as "capture a photo").

Which package helps to exchange data between two applications?

Although you can design your own system for doing data transfers in your app, you should consider using Android's sync adapter framework. This framework helps manage and automate data transfers, and coordinates synchronization operations across different apps.


1 Answers

This worked for me:

  <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" />
       <category android:name="android.intent.category.DEFAULT" />
       <data android:mimeType="*/*" />
  </intent-filter>
like image 81
Raj Avatar answered Sep 23 '22 18:09

Raj