百度地图4.1_1开发教程(7)截图工具类

在我项目中有个奇葩的需求是,要求给当前地图截图,然后分享给其他人。容我啰嗦几句,现在分享位置都是分享个坐标,别人可以在其他地图打开,偏偏我们项目特么的有这么个奇葩需求,那只能彪代码了。

在本文中将会涉及到:
获取系统时间 
百度地图截图工具类的使用方法 
创建文件夹的方法 
保存全局变量的方法
本文已更新1次。

首先要定义常量,因为保存的路径是固定的,只有文件名不同,所以:

新建一个常量类,用来保存路径等
/**
     * 保存截图的路径
     */
    public static String PATH =  "/mnt/sdcard/" + "DCIM/Screenshots/";
    public static String PICNAME = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis())); // 文件名,根据系统时间生成

import android.graphics.Bitmap;

import com.mymap.constant.Constant;
import com.mymap.constant.GlobalVariable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 截图
 * Created by kowal on 2016/12/5.
 */
public class ScreenshotsUtil
{
    /**
     * 百度地图截图工具类
     * @param snapshot 图片对象
     * @return bitmap
     */
    public static Bitmap getScreenshots(Bitmap snapshot)
    {
        if ( !FileUtils.isFolderExists(Constant.PATH)) { // 判断路径是否存在,不存在则创建,创建失败返回false
            return null;
        }
        File file = new File(Constant.PATH + Constant.PICNAME + ".png"); 
        FileOutputStream out;
        try
        {
            out = new FileOutputStream(file);
            if (snapshot.compress(
                    Bitmap.CompressFormat.PNG, 100, out))
            {
                out.flush();
                out.close();
            }
            GlobalVariable.setSharePicname(Constant.PATH + Constant.PICNAME + ".png"); // 保存上次截图的文件名,便于分享时获取路径
            return snapshot;

        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
            return null;
        } catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }
}
FileUtil代码:
    /**
     * 建立多级目录
     */
    public static boolean  isFolderExists(String strFolder) {
        File file = new File(strFolder);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }
使用方法也很简单:
调用百度地图的 
mBaiduMap.snapshot(new BaiduMap.SnapshotReadyCallback()
                        {
                            public void onSnapshotReady(Bitmap snapshot)
                            {
                            } 
                        });

这里放上示例代码:

mBaiduMap.snapshot(new BaiduMap.SnapshotReadyCallback()
                        {
                            public void onSnapshotReady(Bitmap snapshot)
                            {
                                Bitmap shot = ScreenshotsUtil.getScreenshots(snapshot); // 获取截图的bitmap对象
                                if (shot != null)
                                {
                                    String imagePath = GlobalVariable.getSharePicname(); //截图成功,得到文件路径及文件名
                                    if (imagePath != null)
                                    {
//                                    //由文件得到uri
                                    Intent shareIntent = new Intent();
                                    shareIntent.setAction(Intent.ACTION_SEND);
                                    shareIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
                                    shareIntent.setType("image/*");
                                    startActivity(Intent.createChooser(shareIntent, getString(R.string.share_to)));
                                    }
                                } else
                                {
                                showToast("保存截图失败:未知的错误");
                                }
                                showToast("截图已保存在:" + Constant.PATH + Constant.PICNAME);
                            }
                        });

ok,到此位置基本完成了。这里送上一个保存全局变量的方法,很简单:

    private static String SHARE_PICNAME;

    public static String getSharePicname()
    {
        return SHARE_PICNAME;
    }

    public static void setSharePicname(String sharePicname)
    {
        SHARE_PICNAME = sharePicname;
    }
缺点也很明显,当退出APP后,也会同时消除之前保存的变量。如果需要持久化,可以考虑其他方法了。如果对你有帮助,请为我点赞,谢谢。

今天看了一下昨天的代码,又对其封装了一下,文末会给下载地址。
对于分享的代码,我认为还可以封住,上代码:
    /**
     * 分享图片
     */
    public static boolean share(Context context, String imagePath)
    {
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imagePath);
        shareIntent.setType("image/*");
        context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.share_to)));
        return true;
    }

如此,我们直接调用就可以了
/**
* 截图和分享
*/
private void screenShotAndShare()
{
mBaiduMap.snapshot(new BaiduMap.SnapshotReadyCallback()
{
public void onSnapshotReady(Bitmap snapshot)
{
Bitmap shot = ScreenshotsUtil.getScreenshots(snapshot); // 获取截图的bitmap对象
if (shot != null)
{
ScreenshotsUtil.share(activity, GlobalVariable.getSharePicname());
} else
{
showToast(“保存截图失败:未知的错误”);
}
showToast(“截图已保存在:” + Constant.PATH + Constant.PICNAME);
}
});
}

附上下载地址http://download.csdn.net/detail/u012552275/9704000


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值