自己写的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);
}}
}