1

I am building an app that allows people to share apps with each other. I am already able to do this from inside this app (I show the installed apps and the user can select the ones he wants to send), but now I would like to share apks that are not installed, so the apk is just on the downloads folder or something similar.

Imagine that you have an apk in the Download folder for example, I would like to be able to by pressing Share, to go to inside my app and send that apk. It is just like any other share (images,etc).

I was able to make my app to pop up on the list of apps that can share that type of file (in this case .apk), like on this picture (bellow) by adding this to my manifest inside one of the activities tags:

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

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

    <data android:mimeType="application/vnd.android.package-archive" />
</intent-filter>

Share image list

Now the problems are :

1 - How do I know the filepath of the file that the person pressed share?

2 - How do I pass that filepath to my app if the intent is being received on the manifest ?

PS: Notice that the goal is to share an apk from any folder on the device by pressing share (long press and then share - depends on the device).

Thanks in advance for all of the help.

Community
  • 1
  • 1
Steven Smith
  • 111
  • 1
  • 2
  • 11
  • An `ACTION_SEND` implementation is required, by the protocol, to accept any sort of `Uri` (in your case, so long as it appears to point to an APK). That `Uri` *might* have `file` as the scheme, in which case `getPath()` on that `Uri` would be a path to the file (where you get the `Uri` from `getIntent().getData()`). However, that `Uri` could have `content`, `http`, `https`, etc. as the scheme, and in those cases there is no "filepath". – CommonsWare Dec 27 '16 at 17:50
  • @CommonsWare I managed to get the filepath with the getClipData the problem is that it is only available from the api 16 forward. For the ones before, the getData() is always null, so i don't really know how to do it :( Also i tried debugging and checking what is inside the Uri and there is nothing that i need apparently, only if that doesn't show on debug – Steven Smith Dec 28 '16 at 15:51
  • Whoops, sorry, I was thinking of other actions, like `ACTION_VIEW`. The `Uri` for `ACTION_SEND`, if there is one, will be in the `EXTRA_STREAM` extra. – CommonsWare Dec 28 '16 at 15:54
  • Not working either. It returns null. It is weird, because i think that we should be able to share apk files on other versions before api 16... – Steven Smith Dec 28 '16 at 16:11
  • wait, how do you think i could apply that EXTRA_STREAM? Now i just did getStringExtra(Intent.EXTRA_STREAM) and it "refreshes" my intent and then i have the filepath on the extras :s, maybe i am just applying it the wrong way – Steven Smith Dec 28 '16 at 17:09
  • I ended up getting it. Thank you for your help @CommonsWare . If you want you can answer and i will accept it, since all of the other answers are wrong. – Steven Smith Jan 02 '17 at 16:25
  • I recommend that you answer your own question, showing what you did that worked. – CommonsWare Jan 02 '17 at 16:45

3 Answers3

1

You must have added the intent filter on some activity. That activity will be receiving the intent which you can access using Intent intent = getIntent() method. See this doc. That answers part 2.

For the first part, I think you can do intent.getData().getPath()

gitter
  • 1,706
  • 1
  • 20
  • 32
  • the problem is that `getData()` is returning null and i can't use `getClipData()` because it requires minimum api 16 and my minimum is 9 and i can't change it – Steven Smith Dec 28 '16 at 16:26
0

this is my code for sharing data when i click the shareContentProvider I share image and text with it

        Uri imageUri;
        Intent intent;

        imageUri = Uri.parse("android.resource://" + getPackageName() + "/drawable/" + "icon");
        intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_TEXT, "#periodicTable");
        intent.putExtra(Intent.EXTRA_STREAM, imageUri);
        intent.setType("image/*");
        startActivity(intent);
0

Sorry for answering to my own question, but maybe this will help someone in the future. I was debugging and looking inside the extras if there was anything, and nothing showed up. The solution was doing (ArrayList<Uri>) getIntent().getExtras().get("android.intent.extra.STREAM"); and this would "refresh" the Extras from the intent. I don't know why these extras don't show up initially and only after i get the extra stream, but it is working. I used an arraylist of uri's because in this case i was trying with send_multiple. If you are only using SEND then you don't need the ArrayList part.

Steven Smith
  • 111
  • 1
  • 2
  • 11