Trying to provide an image for apps calling GET_CO

2019-04-10 23:41发布

问题:

I am looking for a solution for quite a while and I am getting frustrated more and more.

I want my app to be able to provide content for other apps. For example: When using GoogleMail app you can add an attachment. A dialog will be opened showing all apps that can provide an file for the attachment. My app is in the dialog already, because I defined an activity with an intent-filter like this:

    <intent-filter >
            <action android:name="android.intent.action.GET_CONTENT" />
            <action android:name="android.intent.action.PICK" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.OPENABLE" />
            <data android:mimeType="image/*" />
        </intent-filter>

So when you pick my app, it is starting the activity. The activity allows the user to select a photo and writes it to SD-card. So far so good. But how do I return it correctly? Because the attachment in GoogleMail is never made. I am returning it this way:

if(file!=null)
{
  Intent resultIntent=new Intent();
  resultIntent.setType("image/jpg");  

  Uri uri=Uri.fromFile(file);

  //uri=Uri.parse(uri.toString().replace("file", "content"));
  resultIntent.putExtra(Intent.EXTRA_STREAM, uri); 
  resultIntent.putExtra(Intent.EXTRA_UID, uri); 
  resultIntent.setData(uri);
  setResult(RESULT_OK, resultIntent);
  finish();
}

I would be so so so so happy if anybody could help me. I believe it is a simple thing I am doing wrong, but I can't find anything that helps me. :(

Best regards

回答1:

You'll probably need to implement a stream provider. There really isn't any good guide out there, but this is the closest I could find.