简体   繁体   中英

Android: Taking screenshot of a View, Saving the image and retrieving it

I am basically trying trying to capture a screenshot of a Relative Layout in my Activity and save it as a PNG Image. Then I need to retrieve it as a Bitmap file. I wrote some code to get this done but sadly the screenshot is not saved on my device.

XML

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Other views -->

</RelativeLayout>

Code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_view);

        //The view that needs to be captured as an image
        parent = (RelativeLayout) findViewById(R.id.parent);

        Bitmap image = getBitmapFromView(parent);
}

public static Bitmap getBitmapFromView(View view) {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
        Bitmap returnBitmap = null;

        try {

            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

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

            File imageFile = new File(mPath);

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

            //Retrieve the image as a Bitmap and return it
            if(imageFile.exists()){
                returnBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
            }

        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
        }
        return returnBitmap;
    }

The above code doesn't work. No errors but the view is not captured and saved. Can someone point the problem or give me a better solution please?

  1. First assign an id to the view concerned

  2. Get a reference using findViewById() Then follow this code snippet

//Define a bitmap with height and width of the View

Bitmap viewBitmap = Bitmap.createBitmap(v.getWidth(),v.getHeight(),Bitmap.Config.RGB_565);

Canvas viewCanvas = new Canvas(viewBitmap);

//get background of canvas
Drawable backgroundDrawable = v.getBackground();

if(backgroundDrawable!=null){
backgroundDrawable.draw(viewCanvas);//draw the background on canvas;
}
else{
viewCanvas.drawColor(Color.GREEN);
//draw on canvas
v.draw(viewCanvas) 
}

//write the above generated bitmap  to a file
String fileStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        OutputStream outputStream = null;
        try{
            imgFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),fileStamp+".png");
            outputStream = new FileOutputStream(imgFile);
            b.compress(Bitmap.CompressFormat.PNG,40,outputStream);
            outputStream.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }

Replace PNG with JPEG

Don't forget to add this permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM