截图后如何使图像背景变白?

时间:2019-11-26 08:28:53

标签: android share screenshot

我想截图框架布局并使用intent分享。问题是截图后,我的图像背景变成了黑色。任何人都可以帮助解决这个问题。我想使背景变为正常的白色。

这是我的代码

public void sharePetrol()
{
    bitmap = screenShot(layPetrol);
    shareBitmap(bitmap);
}

public Bitmap screenShot(View view)
{
    view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY));
    view.layout((int) view.getX(), (int) view.getY(), (int) view.getX() + view.getMeasuredWidth(), (int) view.getY() + view.getMeasuredHeight());
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache(true);

    Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

    return createBitmap;
}

public void shareBitmap(Bitmap bitmap)
{
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    String fileName = now + ".jpg";
    String directoryPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";

    File directory = new File(directoryPath);

    if(!directory.exists())
    {
        directory.mkdir();
    }

    File imageFile = new File(directoryPath, fileName);

    try
    {
        FileOutputStream outputStream = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        outputStream.flush();
        outputStream.close();

        Uri uri = Uri.fromFile(imageFile);

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("image/jpeg");
        intent.putExtra(Intent.EXTRA_STREAM, uri);

        try
        {
            startActivity(Intent.createChooser(intent, "Share Screenshot"));
        }
        catch (ActivityNotFoundException e)
        {
            Toast.makeText(PetrolActivity.this, "No App Available", Toast.LENGTH_SHORT).show();
        }
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

`

这是我截屏后得到的结果 enter image description here

2 个答案:

答案 0 :(得分:1)

我已经使用这种方法来处理我的问题。

  public Bitmap screenShot(View view)
    {
    view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY));
    view.layout((int) view.getX(), (int) view.getY(), (int) view.getX() + view.getMeasuredWidth(), (int) view.getY() + view.getMeasuredHeight());
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache(true);
    view.setBackgroundColor(ContextCompat.getColor(this, color.white)); //set background color

    Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

    return createBitmap;
}

答案 1 :(得分:1)

您可以尝试通过添加以下3行来使用Canvas设置背景颜色:

public Bitmap screenShot(View view)
{
    view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY));
    view.layout((int) view.getX(), (int) view.getY(), (int) view.getX() + view.getMeasuredWidth(), (int) view.getY() + view.getMeasuredHeight());
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache(true);

    Bitmap createBitmap = Bitmap.createBitmap(view.getDrawingCache());
    Canvas canvas = new Canvas(createBitmap); // Add these lines
    canvas.drawColor(Color.WHITE); // Add these lines
    view.draw(canvas); // Add these lines
    view.setDrawingCacheEnabled(false);

    return createBitmap;
}