关于屏幕截图的一些事儿

实现截图并上传到服务器

关于屏幕截图的一些事儿_第1张图片
Paste_Image.png

截图

private static Bitmap takeScreenShot(Activity activity) {
    // View是你需要截图的View
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b1 = null;
    try {
        b1 = view.getDrawingCache();
    } catch (OutOfMemoryError e) {
        // TODO: handle exception
    }
    // 获取状态栏高度
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    System.out.println(statusBarHeight);
    // 获取屏幕长和高
    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    int height = activity.getWindowManager().getDefaultDisplay().getHeight();
    // 去掉标题栏
    // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
    view.destroyDrawingCache();
    return b;
}

序列化到本地

public void saveBitmapFile(Bitmap bitmap) {
    Log.e(TAG, "保存图片");
    file = new File("/sdcard/DCIM/Screenshots/", "bg");
    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();
        Log.i(TAG, "已经保存");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

图片上传

    //上传图片
    private void updataImage(final String imageurl) {
        String token = SpUtils.getString(this, "TOKEN");
        String userid = SpUtils.getString(this, "USERID");
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        OkHttpClient mOkHttpClient = new OkHttpClient();
        File file = new File(imageurl);
        builder.addFormDataPart("request", file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
        //这几个是参数
        builder.addFormDataPart("token", userid);
        builder.addFormDataPart("userid", token);
        MultipartBody requestBody = builder.build();
        final Request request = new Request.Builder()
                .url(Define.Server + "uploadImgs.action")
                .post(requestBody)
                .build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "onFailure: " + e.toString());
                ToastUtil.showShort(PartTimeJobDetailsActivity.this, "图片上传失败");
                return;
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Log.d(TAG, "onResponse: " + json);
                ToastUtil.showShort(PartTimeJobDetailsActivity.this, "图片上传成功");
                Log.i("LOGIN", json);
                Gson gson = new Gson();
                ResultBean result = gson.fromJson(json, ResultBean.class);
                if (result == null) {
                    return;
                }
                if (result.isSucceed()) {
                    Type objectType = new TypeToken>>() {
                    }.getType();
                    ResultBean> updataImageBean = gson.fromJson(json, objectType);
                    List data = updataImageBean.getData();
                    shareIMG = Define.ImageServer + data.get(0);
                    share();
                } else {
                    //showShortToast("图片上传失败", 3);
                    return;
                }
            }
        });
    }

你可能感兴趣的:(关于屏幕截图的一些事儿)