ActionBarSherlock学习笔记——ShareActionProvider

ShareActionProvider

1.在menu中加入布局

1 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
2 
3     <item
4         android:id="@+id/menu_item_share_action_provider_action_bar"
5         android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
6         android:showAsAction="always"/>
7 
8 </menu>

2.在设置ShareActionProvider

 1 @Override
 2     public boolean onCreateOptionsMenu(Menu menu) {
 3         // TODO Auto-generated method stub
 4         /**
 5          * 解析menu布局文件
 6          */
 7         getSupportMenuInflater().inflate(R.menu.main, menu);
 8         /**
 9          * 获得分享item
10          */
11         MenuItem item = menu
12                 .findItem(R.id.menu_item_share_action_provider_action_bar);
13         /**
14          * 获得share对象
15          */
16         ShareActionProvider provide = (ShareActionProvider) item
17                 .getActionProvider();
18         /**
19          * 添加历史记录选项
20          */
21         provide.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
22         /**
23          * 添加跳转Intent
24          */
25         provide.setShareIntent(getShareIntent());
26         return super.onCreateOptionsMenu(menu);
27     }

3.设置Intent

 1     /**
 2      * 获得跳转的Intent
 3      * 
 4      * @return
 5      */
 6     private Intent getShareIntent() {
 7         Intent intent = new Intent(Intent.ACTION_SEND);
 8         intent.setType("image/*");
 9         Uri uri = Uri.fromFile(getFileStreamPath("Share.png"));
10         intent.putExtra(Intent.EXTRA_STREAM, uri);
11         return intent;
12     }

4.获得图片

 1     /**
 2      * 将私有的图片转化为public,将raw中的图片,复制到 /data/data/cn.itcast.action/files/下
 3      * 其中的Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
 4      * MODE_WORLD_READABLE:表示当前文件可以被其他应用读取; MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入
 5      */
 6     private void copyPrivateRawResourceToPubliclyAccessibleFile() {
 7         InputStream inputStream = null;
 8         FileOutputStream outputStream = null;
 9         try {
10             inputStream = getResources().openRawResource(R.raw.robot);
11             outputStream = openFileOutput("Share.png",
12                     Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
13             byte[] buffer = new byte[1024];
14             int length = 0;
15             try {
16                 while ((length = inputStream.read(buffer)) > 0) {
17                     outputStream.write(buffer, 0, length);
18                 }
19             } catch (IOException ioe) {
20                 /* ignore */
21             }
22         } catch (FileNotFoundException fnfe) {
23             /* ignore */
24         } finally {
25             try {
26                 inputStream.close();
27             } catch (IOException ioe) {
28                 /* ignore */
29             }
30             try {
31                 outputStream.close();
32             } catch (IOException ioe) {
33                 /* ignore */
34             }
35         }
36     }

 

转载于:https://www.cnblogs.com/qinghuaideren/archive/2013/05/08/3066489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值