android - 请问如何截屏?
问题描述
您好,我有一个截取屏幕截图的代码,然后分享它并且它的工作非常好但是我想从顶部和底部裁剪这个屏幕截图。我希望对我有所帮助,因为我是编码新手。这是我使用它的代码:
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
shareIt();
}
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
private void saveBitmap(Bitmap bitmap) {
imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png"); ////File imagePath
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
private void shareIt() {
Uri myUri = Uri.fromFile(imagePath);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/*");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.putExtra(Intent.EXTRA_STREAM, myUri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
displayResults();
}
解决方案
如果你想使用一个库,你可以实现这个库。并按照此代码
来自 ArthurHub/Android-Image-Cropper
AndroidManifest.xml
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat"/> <!-- optional (needed if default theme has no action bar) -->
启动 CropImageActivity
// start picker to get image for cropping and then use the image in cropping
activity
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
// start cropping activity for pre-acquired image saved on the device
CropImage.activity(imageUri)
.start(this);
// for fragment (DO NOT use `getActivity()`)
CropImage.activity()
.start(getContext(), this);
覆盖 onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
推荐阅读
- maven - jmeter-maven-plugin 存储库配置
- r - 在 r 中找到一个句子与许多其他句子的余弦相似度
- reactjs - 有没有办法使用material-ui创建一个带有线性进度的按钮?
- python - 如何在不冻结 GUI 的情况下训练 Keras 模型?
- android - 如何在 Android TV 应用上禁用 Playback ControlGlue?
- java - 通过 Gitlab 推送 JavaDoc
- go - 如何在代码提交和通过drone.io构建应用程序时在代码中提供所需的go模块?
- python - 使用请求时出现 MissingSchema 异常
- python - 如何使用 xarray.apply_ufunc() 将 1D 中值滤波器应用于 3D DataArray
- javascript - 如何在 php 中使用 jquery 更新当前日期