0

I am working on an android project in which I want to take the screen shot of current activity and save that as .jpeg file in my android device.

When I use instance of FileOutputStream to write the stream of bitmap image into a file, it gives me following error

Argument 3 cannot converted from 'Java.IO.FileOutputStream' to 'System.IO.Stream'

My code

 private void ShareButton_Click(object sender, EventArgs e)
    {
        //create a bitmap screen capture
        View screen = FindViewById(Resource.Layout.AboutImage);
        Bitmap bitmap = Bitmap.CreateBitmap(screen.GetDrawingCache(false));
        screen.SetWillNotCacheDrawing(true);
        image = new File(directory, "Eco_Friendly " + mc.identifier);

        FileOutputStream outputstream = new FileOutputStream(image);

        int quality = 100;
        bitmap.Compress(Bitmap.CompressFormat.Jpeg, quality, outputstream);//Here is error
    }

Q:How to solve this problem?

mdadil2019
  • 807
  • 3
  • 12
  • 29
  • [How to programmatically take a screenshot in Android?](http://stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot-in-android) – huse.ckr Sep 05 '16 at 08:50

2 Answers2

1

Try this code:

package com.screen.shots;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class CaptureScreenShots extends Activity {
    LinearLayout L1;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_shots);
         L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
            Button but = (Button) findViewById(R.id.munchscreen);
            but.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    View v1 = L1.getRootView();
                    v1.setDrawingCacheEnabled(true);
                    Bitmap bm = v1.getDrawingCache();
                    BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
                    image = (ImageView) findViewById(R.id.screenshots);
                    image.setBackgroundDrawable(bitmapDrawable);
                }
            });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.screen_shots, menu);
        return true;
    }

}

Result:

enter image description here

LychmanIT
  • 695
  • 6
  • 13
0

like this

View root = getWindow().getDecorView().getRootView();
root.setDrawingCacheEnabled(true);
root.buildDrawingCache();
Bitmap snapshot = root.getDrawingCache();

at last, you should call this

root.destroyDrawingCache();
yao liu
  • 121
  • 8