Android实现保存截取屏幕图片到指定文件 Android实现屏幕截图并保存截图到指定文件
zhangphil 人气:0Android屏幕截图并保存截取屏幕的图片到指定文件,具体内容如下
注意要添加权限:
<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; } }
加载全部内容