Search code examples
androidandroid-intentandroid-activityadb

Starting share activity from ADB shell


I'm starting a sharing activity from ADB shell.

Specifically I am trying to achieve the same as this (working) java-snippet:

    File dir = Environment.getExternalStorageDirectory();
    File yourFile = new File(dir, "/_tmp/1.jpg");

    Uri yourFileUri = Uri.fromFile(yourFile);

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    sendIntent.setType("image/*");
    sendIntent.putExtra(Intent.EXTRA_STREAM, yourFileUri);
    sendIntent.setPackage("com.snapchat.android");
    startActivity(sendIntent);

Here is the commands I am using:

adb shell am start -a android.intent.action.SEND -t image/jpeg --es android.intent.extra.STREAM file:///storage/emulated/0/_tmp/1.jpg com.snapchat.android --grant-read-uri-permission

The issue I am currently facing is that the snapchat app will open and load, flash black for a second but fails to load the image.

I have checked that the path to the image is working by issuing the following commands, and opening the image in the android gallery:

adb shell am start -t image/jpeg -d file:///storage/emulated/0/_tmp/1.jpg

What am I doing wrong?

Edit:

Using the command:

adb shell am start -a android.intent.action.SEND -t image/jpeg --es android.intent.extra.STREAM file:///storage/emulated/0/_tmp/1.jpg com.snapchat.android/com.snapchat.android.LandingPageActivity --grant-read-uri-permission

Gives the same result, however I compared it to the actual call in DDMS, see the difference here:

Log by sharing manually through the phone:

10-19 18:01:54.020: D/HtcShareActivity(21561): onItemClick: position=0 activity=com.snapchat.android/com.snapchat.android.LandingPageActivity

10-19 18:01:54.061: I/ActivityManager(724): START u0 {act=android.intent.action.SEND typ=image/jpeg flg=0x1 cmp=com.snapchat.android/.LandingPageActivity (has clip) (has extras)} from uid 10078 on display 0

Log by using the ADB command:

10-19 18:04:29.096: I/ActivityManager(724): START u0 {act=android.intent.action.SEND typ=image/jpeg flg=0x10000000 cmp=com.snapchat.android/.LandingPageActivity (has extras)} from uid 2000 on display 0

As you can see the (has clip) is not present in the ADB call.

Could it be that --grant-read-uri-permission isn't "working" or at least not giving sufficient permission?

How do I test and ultimately solve this?


Solution

  • It turned out I wasn't sending any real image data, I was simply sending a string by using --es, however the a URI was needed, and therefore I should have used --eu.

    Updating the following command:

    adb shell am start -a android.intent.action.SEND -t image/jpeg --es android.intent.extra.STREAM file:///storage/emulated/0/_tmp/1.jpg com.snapchat.android/com.snapchat.android.LandingPageActivity --grant-read-uri-permission
    

    To:

    adb shell am start -a android.intent.action.SEND -t image/jpeg --eu android.intent.extra.STREAM file:///storage/emulated/0/_tmp/1.jpg com.snapchat.android/com.snapchat.android.LandingPageActivity --grant-read-uri-permission
    

    Solved my issue.

    It was not due to leaving out declaration of the main activity that @Dus suggested. (Good thought though.)

    The command can be simplified all the way down to:

    adb shell am start -a android.intent.action.SEND -t image/jpeg --eu android.intent.extra.STREAM file:///storage/emulated/0/_tmp/1.jpg com.snapchat.android