Android 分享地图截图至微信时候的坑

         自己写的APP,希望在微信上分享自己APP的截图,在截图的时候会遇到2个坑。1使用地图API自带的截图,得到的仅仅就是地图,你在地图之外(不是对地图本身的操作)所写或者画的图像都不能够得到。2.如果使用系统自带的截屏功能,得到的只能是自己在地图之外所写所画的内容,地图则是一片黑色。
         这时候需要做两次截图,第一次使用Map自己带有的截图方法,得到一个bitmap,然后再将Bitmap图贴到当前布局的上方,你写文字或者画图内容的下方。这时候在调用系统的截图,这时候就能够截取到想要的图片啦。
        高德地图自带的截图函数如下:

aMap.getMapScreenShot(new AMap.OnMapScreenShotListener() {
@Override
public void onMapScreenShot(Bitmap bitmap) {

        }

        @Override
        public void onMapScreenShot(Bitmap bitmap, int status) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            if(null == bitmap){
                return ;
            }

// try {
// createFolder.createFolder();
// // 获取内置SD卡路径
// String sdCardPath = Environment.getExternalStorageDirectory().getPath();
// // 图片文件路径
// //在这里再创建个文件夹,如果文件夹不存在就穿点文件夹。
// String filePath = sdCardPath+ “/pictureFolder/screenshot.png”;
// FileOutputStream fos = new FileOutputStream(filePath);
// boolean b = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
// bimpfortime[0] = bitmap;
// try {
// fos.flush();
// } catch (IOException e) {
// e.printStackTrace();
// }
// try {
// fos.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
bimpfortime[0] = bitmap;
Message msg = Message.obtain();
msg.arg2 = 300;
msg.what = 2; //标志消息的标志
handler.sendMessage(msg);
}
});
系统自带的截屏函数:
public static void captureSctrrn(){
View dView = PathBack.getInstance().getWindow().getDecorView();
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
//
if (bitmap != null) {
try {
createFolder.createFolder();
// 获取内置SD卡路径
String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
// 图片文件路径
//在这里再创建个文件夹,如果文件夹不存在就穿点文件夹。
//
String filePath = sdCardPath+ “/pictureFolder/screenshot.png”;
File file = new File(filePath);
FileOutputStream os = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
}
}
接下来就是将图片分享给微信了,对于android7.0以下的版本直接使用 uri = Uri.fromFile(picFile);进行数据传递,但是对于android7.0以上版本,为了安全性,不能使用这种形式进行Uri传递。这时候要使用provider,这个讲的人也比较多。但是会发现,我们什么都按照说明做了,但是还是得到反馈为“获取资源失败”,这是因为,android6.0以后需要动态的加入权限,像以前的直接在Manifest中加入文件的方法不行了。需要静态加入权限后动态再次的加入权限。代码如下(不知道是copy的那位大神的了):
public void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

    }}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值