Social sharing on mobile

前端 未结 2 560
[愿得一人]
[愿得一人] 2021-01-01 05:06

On a website, one can use a social sharing javascript library like addthis in order to propose share buttons to the user without having to program everything from scratch.

相关标签:
2条回答
  • 2021-01-01 05:08

    On Android we have Intents for this. If you like to give the user an opportunity to share something, you can fire up an intent like this for example:

    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg") // might be text, sound, whatever
    share.putExtra(Intent.EXTRA_STREAM, pathToPicture);
    startActivity(Intent.createChooser(share, "share"));
    
    0 讨论(0)
  • 2021-01-01 05:11

    pathToPicture in previous answer is vague. It should be an Uri. See Android docs

    More elaborate example:

    String path = "/mnt/sdcard/dir1/sample_1.jpg";
    Intent share = new Intent(Intent.ACTION_SEND);
        MimeTypeMap map = MimeTypeMap.getSingleton(); //mapping from extension to mimetype
        String ext = path.substring(path.lastIndexOf('.') + 1);
        String mime = map.getMimeTypeFromExtension(ext);
        share.setType(mime); // might be text, sound, whatever
        Uri uri = Uri.fromFile(new File(path));
        share.putExtra(Intent.EXTRA_STREAM,uri);//using a string here didnt work for me
        Log.d(TAG, "share " + uri + " ext:" + ext + " mime:" + mime);
        startActivity(Intent.createChooser(share, "share"));
    
    0 讨论(0)
提交回复
热议问题