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

来源:互联网 发布:最好的英汉翻译软件 编辑:程序博客网 时间:2024/05/16 06:05

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

         文字很简单,通过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
原创粉丝点击