Social sharing on mobile

我是研究僧i 提交于 2019-11-30 10:27:41

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"));

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"));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!