如何拍摄,裁剪和共享屏幕截图?

时间:2018-09-07 02:07:10

标签: android android-studio share screenshot

我有一个代码要保存,保存然后共享一个活动的屏幕截图。我的问题是我只想把活动作为中心,所以我添加了这段代码

private  Bitmap cropBitmap(Bitmap bitmap)  {
            Bitmap bm = Bitmap.createBitmap(bitmap, 10, 10,  500,  500);
            return bm;
        }

但是它仍然是原始屏幕截图,请帮助我尝试许多解决方案,但由于我在编码方面非常业余,因此我似乎不知道如何应用此解决方案。因此,希望对我有所帮助,请根据我的代码提供正确的解决方案。非常感谢

    @Override
    public void onClick(View v) {
        Bitmap bitmap = takeScreenshot();
        cropBitmap(bitmap);
        saveBitmap(bitmap);
        shareIt();
    }


    public Bitmap takeScreenshot() {
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);
        return bitmap;
    }

    private  Bitmap cropBitmap(Bitmap bitmap)  {
        Bitmap bm = Bitmap.createBitmap(bitmap, 10, 10,  500,  500);
        return bm;
    }

    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(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("image/*");
        sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        String shareBody = "My highest score with screen shot";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Catch score");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, myUri);

        startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
});


    displayResults();

1 个答案:

答案 0 :(得分:0)

尝试这样:

Bitmap bitmap = takeScreenshot();
Bitmap bitmapCropped = cropBitmap(bitmap);
saveBitmap(bitmapCropped );