Android 请问如何裁剪截图?

Android 请问如何裁剪截图?,android,image,android-studio,screenshot,crop,Android,Image,Android Studio,Screenshot,Crop,你好,我有一个代码采取截图,然后分享它和它的工作非常好 但是我想从顶部和底部截图。 我希望能帮助我,因为我是新的编码。 这是我使用它的代码: share.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Bitmap bitmap = takeScreenshot(); saveBitmap(bitmap);

你好,我有一个代码采取截图,然后分享它和它的工作非常好 但是我想从顶部和底部截图。 我希望能帮助我,因为我是新的编码。 这是我使用它的代码:

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图像裁剪器

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) -->
覆盖ActivityResult

@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();
        }
      }
    }

你必须先搜索再问。我相信你没有。可能是复制品
@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();
        }
      }
    }