这篇文章主要介绍Android如何实现屏幕截图并保存截图到指定文件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
Android屏幕截图并保存截取屏幕的图片到指定文件,具体内容如下
注意要添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
完整代码:
package zhangphil.test; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Environment; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import android.view.View; import android.widget.Toast; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class JavaActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.drawable_activity); AppCompatActivity activity = this; findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "zhangphil.jpg"); Bitmap bitmap = screenShot(activity); try { if (!file.exists()) file.createNewFile(); boolean ret = save(bitmap, file, Bitmap.CompressFormat.JPEG, true); if (ret) { Toast.makeText(getApplicationContext(), "截图已保持至 " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } }); } /** * 保存图片到文件File。 * * @param src 源图片 * @param file 要保存到的文件 * @param format 格式 * @param recycle 是否回收 * @return true 成功 false 失败 */ public static boolean save(Bitmap src, File file, Bitmap.CompressFormat format, boolean recycle) { if (isEmptyBitmap(src)) return false; OutputStream os; boolean ret = false; try { os = new BufferedOutputStream(new FileOutputStream(file)); ret = src.compress(format, 100, os); if (recycle && !src.isRecycled()) src.recycle(); } catch (IOException e) { e.printStackTrace(); } return ret; } /** * 获取当前屏幕截图,不包含状态栏(Status Bar)。 * * @param activity activity * @return Bitmap */ public static Bitmap screenShot(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int statusBarHeight = getStatusBarHeight(activity); int width = (int) getDeviceDisplaySize(activity)[0]; int height = (int) getDeviceDisplaySize(activity)[1]; Bitmap ret = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return ret; } public static float[] getDeviceDisplaySize(Context context) { Resources resources = context.getResources(); DisplayMetrics dm = resources.getDisplayMetrics(); int width = dm.widthPixels; int height = dm.heightPixels; float[] size = new float[2]; size[0] = width; size[1] = height; return size; } public static int getStatusBarHeight(Context context) { int height = 0; int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { height = context.getResources().getDimensionPixelSize(resourceId); } return height; } /** * Bitmap对象是否为空。 */ public static boolean isEmptyBitmap(Bitmap src) { return src == null || src.getWidth() == 0 || src.getHeight() == 0; } }
以上是“Android如何实现屏幕截图并保存截图到指定文件”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。