Dev
Dev

Reputation: 7046

Receiving Content from Other Apps

I would like to register my app to receive files (of any type, not just images) from other apps. I'm following the Receiving Content from Other Apps tutorial.

I have:

  1. Updated the app's manifest:

        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>
    
  2. Implemented the onCreate() method of the activity:

    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();
    
    if (Intent.ACTION_SEND.equals(action) && type != null) {            
        this.handleSend(intent);
    }
    
  3. Implemented my private handleSend() method:

    void handleSend(Intent intent) {
        Uri fileUri = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
    
        if (fileUri != null) {
             // OK, now?
        }
    }
    

If I print the fileUri, it is something like content://downloads/all_downloads/5. But how can I access its content?

Upvotes: 3

Views: 8050

Answers (2)

tej shah
tej shah

Reputation: 3095

change method as bellow

void handleSend(Intent intent) {
        try {
            Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
            imageShare.setImageURI(imageUri);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006624

As @Selvin indicated, use getContentResolver().openInputStream(fileUri); to get an InputStream on the contents of that ContentProvider Uri. The MIME type should be in the Intent.

Upvotes: 2

Related Questions