javaandroidfileintentfilter

Java How to launch myapp by opening an image?


I'm working on an image editor app and I want to have my app being in the 'Open With ...' list when the user clicks on an image from file manager or ... (Like how 'Photo Editor' app did) :

Click to see the example (Edited)

And the problem is that I don't know how to do such a thing.

I have researched in youtube and I found that it's about the intent filter in the Manifest.xml file.

I already tried adding some <action/> to my intent filter by random but it didn't worked.


Solution

  • Go to the Manifest file and search for your LAUNCHER activity for example SplashActivity

    like this

      <activity
        android:name=".SplashActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
          //this below code help you to get image from other apps if shared
         <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="image/*" />
        </intent-filter>
    
    </activity>
    

    And To get that data in your SplashActivity do this :

     Intent intent = getIntent();
     String action = intent.getAction();
     String type = intent.getType();
     //this will check that you are getting only image type not text
     boolean isImage = Intent.ACTION_SEND.equals(action) && "image/*".equals(type);
    
    if(getIntent().getData() != null && isImage){
     // here you got the data and you can send this data or uri 
     // to your image editor activity
     Uri imageUri = intent.getData();
    }
    

    And you can send that image to the next activity by doing this

    you can store the URI as a string

    Intent nextIntent = new Intent(this, EditorActivity.java);
    nextIntent.putExtra("imageUri", imageUri.toString());
    startActivity(nextIntent)
    

    and then just convert the string back to URI like this in your (EditorActivity)

    Uri myUri = Uri.parse(getIntent().getStringExtra("imageUri"));