利用ShareSDK分享图片

分享单张图片

效果

利用ShareSDK分享图片_第1张图片
图1:预览图片.jpg

利用ShareSDK分享图片_第2张图片
图2:分享图片到微信.jpg

图一通过使用Glide加载的预览图,可通过Glide缓存的图片去分享图片(取出缓存的方法必须执行在子线程,所以用AsyncTask异步任务去管理)

AsyncTask

public class ImgCache {

    private Context context;
    private String url;

    public ImgCache(Context context, String url) {
        this.context = context;
        this.url = url;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
public class AsyncTaskCache extends AsyncTask {

    private CacheListener listener;

    public void setListener(CacheListener listener) {
        this.listener = listener;
    }

    @Override
    protected String doInBackground(ImgCache... imgCaches) {

        try {
            FutureTarget target = GlideApp.with(imgCaches[0].getContext())
                    .load(imgCaches[0].getUrl())
                    .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
            File file = target.get();
            return file.getAbsolutePath();

        } catch (Exception e) {
            Log.e("AsyncTaskCacheException", e.getMessage());
        }

        return "";
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        if (listener != null) {
            listener.cacheFilePath(s);
        }
    }

    public interface CacheListener {
        void cacheFilePath(String cache);
    }
}

构建分享的dialog(在fragment中)

    // onCreateView
    private void getImageFromCache() {

        taskCache = new AsyncTaskCache();

        taskCache.setListener(new AsyncTaskCache.CacheListener() {
            @Override
            public void cacheFilePath(final String cache) {

                SimpleNetWorkPicFragment.this.cache = cache;
                // 加载图片
                GlideApp.with(mContext)
                        .load(cache)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(img);// Argument must not be null

                if (!TextUtils.isEmpty(cache)) {
                    dialog = new ShareDialog(mContext, new ShareDialog.SelectPlatformListener() {
                        @Override
                        public void select(String platform) {
                            ShareUtils.showShareOneImg(platform, cache, mContext);
                        }
                    });
                }
            }
        });

        taskCache.execute(new ImgCache(mContext, Api.IMG_URL + path));
    }

    @Override
    public void onPause() {
        super.onPause();
        // viewPage机制默认加载前后页
        // 如不执行此行代码快速滑动会报Argument must not be null异常
        // 所以此处用了AysyncTask去执行耗时操作
        taskCache.cancel(true);
    }

图二通过传入缓存图片的路径进行分享

    private static OnekeyShare oks = new OnekeyShare();

    public static void showShareOneImg(String platform, final String imagePath, Context context) {

        //指定分享的平台,如果为空,还是会调用九宫格的平台列表界面
        if (platform != null) {
            oks.setPlatform(platform);
        }
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // imagePath是图片的本地路径,Linked-In以外的平台都支持此参数
        // glide取出的图片缓存
        oks.setImagePath(imagePath);//确保SDcard下面存在此张图片
        // 启动分享GUI
        oks.show(context);
    }
if (dialog != null)
    dialog.show(all, Gravity.BOTTOM);
else
    showErrorMsg("复制图片失败,暂时不能转发给好友");

你可能感兴趣的:(利用ShareSDK分享图片)