获取的是当前的截图调用系统分享
//1.首先获取截图
public Bitmap getBitmapByView() {
View dView =getWindow().getDecorView();
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
return bitmap;
}
//2.把截图获取的bitmap做简单的压缩
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 10, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 400) { //循环判断如果压缩后图片是否大于400kb,大于继续压缩(这里可以设置大些)
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
options -= 10;//每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
return bitmap;
}
//3,把压缩过的图片先保存到本地才能调用系统分享出去,因为系统分享的是一个uri,我们需要先把bitmap转为本地file文件
public File bitMap2File(Bitmap bitmap) {
String path = "";
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "flyTrb";//保存到sd根目录下
}
//通知系统更新文件
sysToScan(path);
File appDir = new File(path);
if (!appDir.exists()) {
appDir.mkdir();
}
File f = new File(appDir, System.currentTimeMillis() + ".jpg");
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
bitmap.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
return f;
}
}
//4.更新系统文件夹
public void sysToScan(String filePath) {
//扫描指定文件夹中的文件
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File file = new File(filePath);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
//向系统发送广播
ContractShoreActivity.this.sendBroadcast(intent);
}
//调用方法
Bitmap bitmapByView = getBitmapByView();
compressImage(bitmapByView);
File file = bitMap2File(bitmapByView);
Uri uri = Uri.fromFile(file);
Intent imageIntent = new Intent(Intent.ACTION_SEND);
imageIntent.setType("image/*");
imageIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(imageIntent, "分享"));
安卓7.0版本之后获取文件需在清单文件中添加
<!--获取文件路径-->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myPackage.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
新建xml文件夹
file_paths.xml
<paths>
<!-- 物理路径为Context.getFilesDir() + /files/* -->
<files-path path="files" name="files" />
<!-- 物理路径为Context.getCacheDir() + /files/* -->
<cache-path path="files" name="cache" />
<!-- 物理路径为Environment.getExternalStorageDirectory() + /files/* -->
<external-path path="files" name="external" />
<!-- 物理路径为Context.getExternalFilesDir(String) + /files/* -->
<external-files-path path="files" name="externalfiles"/>
<!-- 物理路径为Context.getExternalCacheDir() + /files/* -->
<external-cache-path path="files" name="externalcache"/>
<!-- 物理路径为`Context.getExternalMediaDirs() + /files/*, 要求API21+ -->
<external-media-path name="externalmedia" path="files" />
</paths>