分享文字和图片到微博、微信等公众平台遇到的问题

        看了网上关于分享的解决方法,现在总结一下。

         文字很简单,通过intent进行参数传递就行。

        图片需要序列化才能分享,这里intent是通过Uri.fromFile(new File())来实现序列化。

 

   public void shareScore(Activity context)
    {   String content = "本次得分" + myScore;
        PictureUtil.savePic(context, PictureUtil.takeScreenShot(context), gameShareName);
        File file = context.getFileStreamPath(gameShareName);
        Uri uri = Uri.fromFile(file);
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        if (uri != null)
        {
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.setType("image/*");
        } else
        {
            shareIntent.setType("text/plain");
        }
        shareIntent.putExtra(Intent.EXTRA_TEXT, content);
        context.startActivity(shareIntent);
    }



   public static boolean savePic(Activity context, Bitmap b, String filename)
    {
        FileOutputStream fos = null;
        try
        {
            fos = ((Activity) context).openFileOutput(filename, Context.MODE_WORLD_READABLE
                    | Context.MODE_WORLD_WRITEABLE);
            if (null != fos)
            {
                b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                try
                {
                    fos.flush();
                    fos.close();
                    return true;
                } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
      }  return false;
    }

 /**
     * 进行截取屏幕
     * 
     * @param pActivity
     * @return bitmap
     */
        public static Bitmap takeScreenShot(Activity pActivity)
    {   View view = pActivity.getWindow().getDecorView(); Rect frame = new Rect();
        view.getWindowVisibleDisplayFrame(frame);
        int stautsHeight = frame.top;
        int width = pActivity.getWindowManager().getDefaultDisplay().getWidth();
        int height = pActivity.getWindowManager().getDefaultDisplay().getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height - stautsHeight, Bitmap.Config.ARGB_8888);
        view.draw(new Canvas(bitmap));
        return bitmap;
    }


       通过以上方法能实现对屏幕截图的图片和文字分享。分享过程中遇到的问题:

       1.文件读取权限的问题,如果存储文件到SD卡上,报错出现java.io.IOException: Permission denied。 该问题出现的可能是:

                    1)没有设置user权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

                    2)文件路径错误:应该在Environment.getExternalStorageDirectory()路径下读写文件。


                    3)真机开发的。。。,在调试过程中SD卡禁止访问,需要与电脑断开USB连接后,SD卡正常插入后读写文件。

        2.分享照片时,需要Uri生成的序列化数据,才能正常分享:

             Uri uri = Uri.fromFile(file);

        3.公共平台是否支持相关的文字图片同事分享的接口。。

 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值