Tuesday, March 3, 2015

How to CORRECTLY take a screenshot using Android MediaProjection API

With the MediaProjection API in Android 5.0 it is possible to: 
capture the contents of the main screen (the default display) into a Surface object, which your app can then send across the network
The process to capture the screen contents is described in MediaProjection API Demo. This demo uses a SurfaceView to show a miniature version of the device's screen creating a Droste effect. But the surface view is not very helpful to actually use the captured screen i.e you can not get the screen capture as an image or a video. If you need to capture the screen as an image you can use and ImageReader object and pass its surface to createVirtualDisplay() method.

The problem is that it is not very straight forward to get the captured screen from ImageReader object either. The image that you get from ImageReader is raw i.e it has stride pixels in the image buffer and you need to take care of them your self. There are many people out there having difficulties getting the image from an ImageReader e.g here, here and here. The solution described in some of these Stackoverflow posts does work but it reduces the image quality significantly and also it is a lot more work and therefore it is slow. 

Below I describe a simpler method to get the image from ImageReader once you have created a virtual display using the ImageReader's surface. For completeness sake I describe the steps for creating a virtual display as well but if you are familiar with them just skip to step 3.

Step 1
To capture the screen contents you need a MediaProjection object. But first you need the user's permission to capture the screen using following code:
MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);startActivityForResult(projectionManager.createScreenCaptureIntent(), PERMISSION_CODE);
This will start a dialog activity to ask user for permission to capture the display contents. 

Step 2
Once user allows screen capture, we can get a MediaProjection object in the onActivityResult() method using the following code.
MediaProjectionManager projectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
mProjection = projectionManager.getMediaProjection(resultCode, data);
The "data" passed to getMediaProjection() call is the intent data passed to onActivityResult() method. 

Step 3
The call to create a virtual display takes width and height of the virtual display and a surface object which will be used to save the device's screen. To the capture the screen as an image or a series of images we use surface of an ImageReader object. Create an ImageReader object as follows:
mImageReader = ImageReader.newInstance(mWidth, mHeight, ImageFormat.RGB_565, 2);
Step 4
Once we have the MediaProjection and ImageReader objects, we can start capturing screen by creating a virtual display using following code:
mProjection.createVirtualDisplay("screen-mirror", mWidth, mHeight, mDensity, flags, mImageReader.getSurface(), null, null);
Step 5
Now we can get the screen capture as image using the following code.
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int offset = 0;
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mWidth;
// create bitmap
Bitmap bmp = Bitmap.createBitmap(mWidth+rowPadding/pixelStride, mHeight, Bitmap.Config.RGB_565);
now the bitmap object bmp has the your screen capture in correct format and you can simply use Bitmap.compress() method to convert it to JPEG, PNG or WEBP format. 
The error that most people make here is that they create bitmap of the wrong size, they create bitmap of same width and height as the virtual display but the problem is that the image you get from ImageReader has some extra pixels in there on each line. So you need to account for those as well when creating the bitmap to copy the pixel data. Therefore when creating bitmap I am passing "mWidth+rowPadding/pixelStride" as width. This is for sake of clarity so it is easy to understand otherwise you can instead pass "rowStride/pixelStride".


Manos said...


I have used code similar to your to capture the screen in Android 5.0. I had a problem in that the user can at any point stop projection by manually going to the notification bar and ending it. Is there a callback so that my app can identify such events and act accordingly? I tried using MediaProjection.Callback but with no success.

Unknown said...

using the method you outlined but I'm still getting the extra size on the right edge of the screen shot. Anything else I need to do?

Bitmap __bmp = Bitmap.createBitmap(__rowStride/__pixelStride, __height, Bitmap.Config.ARGB_8888);

Unknown said...

I have implemented as you suggested by I am getting black images having sizes 2-3 KB, please can you share your code or can help??

Thanks and regards

Unknown said...

What about `flags`? What flags are we supposed to use to capture what's on the screen?

I used `VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR` and I get images of height and width as "-1".

Unknown said...

When MediaProjection permission dialog screen comes up, MainActivity is being called how can we prevent it
I am sorry for my English

Unknown said...

Hey his is not a reply but have u found the solution to this problem I am currently facing the same problem

Unknown said...

Very helpful, thanks for sharing the code.

Angel Claudia said...

We are among the best online companies providing affordable nursing essay writing services. When you nursing writing services, you get the services of experts and specialists in your field.

meldaresearch said...

You need not to worry any more,we are here to offer you the best academic writing
solutions;order quality pre written research papers for sale from us today!

leahmelda@gmail.com said...

Online Biology Paper Writing Services produces different assignments, including Buy Essay Online Services, to help students deal with the Buy Business Reports challenges they face every day.

Ivymelda said...

Medicine Essay Writing are the most outstanding in the writing industry, enabling you to achieve the best Thesis Writing Services. Also the professional writers’ commitments to delivering the top Medicine Essay Writing Services.

Legitimate Custom Research Paper Services said...

You should take some time and evaluate the Custom Essay Writing Services and Cheap Custom Essay Services as most students buy Affordable Online Essay For Sale and get ripped off

London Tigers Security said...

I am thanks for the article post. looking forward to visiting more. London Tigers Security Door Supervisor

Farnek UK said...

Great work I always try to share good information. Farnek.uk

Hot Pics said...

Japanese Celebrities Hot Images, Hot Photos, Hot Pics just visit this website given below.

Hot Pics

Check Mot History said...

Greetings! The very helpful advice in this particular blog post! Providing Us With This Great Knowledge Check Mot History Online

assignmentauthors said...

My aim here is to find out more about the registrations systems that re performed online in some three separate universities getting to understand how efficient these systems of registrations are together with the challenges facing them before coming up with a permanent and long lasting solution. assignment expert

The Tech Helpers said...

Shashka has all the party supplies you need, all in one place, all year long. For birthdays and beyond, no matter what you’re celebrating, this online portal has everything you could ever want when you’re throwing the best party ever. The ease and convenience of the site makes planning and shopping for your party almost as fun as having it! With hundreds of the highest-quality party supplies for all ages and occasions, Shashka.pk is the place to be.

Smart Research USA said...

We all know the main trouble of every student is lack of time! That's why you need to visit this site Homework Help Writing Services As well as this site Assignment Writing Services Online The Website also offers Online Research Paper Services at an affordable cost.

Katherine Flintoff said...

Lahore Smart City is going to be the best choice for commercial, investment and residential point of view. The scheme will have everything to attract national and international investors. In return, investors will get high revenue. On the other hand, the housing society is equipped with state of the art facilities. The facilities are just dream of come true for the people of Lahore. Peace, safety and eco-friendly behaviors Future Development Holding hires and corporate’s with world-class developers, architectures and planners. This Smart City Lahore will have golf clubs and fields designed by experienced and world-recognized designers.

Hosthook Official said...

Are you looking for Cheap Hosting Services Provider Company? Do not worry because Hosthook is one of the leading and famous Web Hosting Services Provider Company in the World. So what are you waiting for just visit our website and choose your ideal package.

williams jones said...

We have notice some very good content here and we nice reading.buy cocaine online and we also want us to read this blog post .order cocaine online if you want to know more about .buy dilaudid online you should read here for more .buy actavis online to make a blog site is not easy so read here how to make blog site .buy peruvian cocaine online with free hosting and more site too.

williams jones said...

Thanks for sharing What an interesting site you have i really take a good look buy cocaine online and you can also take a good look at our blog too for more information buy crack cocaine online we also recommend you to take a good look at this site too they have good content more of the site still want you to take a closer look too as they have really good content thanks for using your time to check on the link .order cocaine online

williams jones said...

buy dank vape online

williams jones said...

What a great content we have here buy blue dream online as we browse we also see this blog too and is pretty good buy purple haze online we also realize that blog are good for business we also bring buy og kush online best blog to visit
buy khalifa kush online thanks for checking on this post

williams jones said...

We have notice some very good content here and we nice reading.buy oxycontin online and we also want us to read this blog post .buy adderall online if you want to know more about .buy percocet online you should read here for more .buy xanax online to make a blog site is not easy so read here how to make blog site .buy norco pills online with free hosting and more site too.

Buy Research Chemical said...

buy research chemicals online
Buy Weed Online
but psychedelic online
Buy Stimulants online
Buy pain relief pills online

Najaf Qureshi said...

Are you searching for Capital Smart City? You want any type of information about this project? Do not worry we are here to help you. Just visit our website or call us on given number.
Call/WhatsApp : +92 331 7627888

Capital Smart City

Nova City Islamabad

Park View City said...

An Upcoming Tourist Destination in Islamabad
Blue world city Islamabad map
park view lahore map
Rudn Enclave Rawalpindi

Buy Research Chemical said...

buy research chemicals online
Buy Wholesale Products
Buy axeity pills online
Buy Weed Online
Buy Cocaine Online USA
Where to buy frog venom online
Buy heroin online
Buy Hybrid Strain Online
Buy indica weed online
buy mercury online
where to order moonrocks online
buy pain relief pills online
buy psychedelic online
buy research chemicals online
buy sativa cannabis online
buy scorpion venom online
buy sleeping pills online
buy snake venoms online
Buy Stimulants online usa
buy toad venoms online
Buy Venoms online

Brooklyn Smith said...

If your university or college teacher gives you an assignment or project which is difficult to solve, then you try out GoAssignmentHelp.com services. We offer superior quality college assignment help, with the team of highly qualified and skilled professionals. GoAssignmentHelp.com introduces you to best assignment writer’s academicians in every field to fulfil your every environmental economics assignment help, essay, project, research, and dissertation need. Our experts strive to reach students studying in distant areas and help them accomplish complex tasks and achieve appreciative grades. Feel free to browse our website and get in touch with our live support as well for further buy assignment online queries. So go ahead register and let us do the rest for you.

carts shop said...

liquid lsd online

cocaine powder

mescaline powder

volkswagen cocaine

colombian cocaine

amazing columbian cocaine

cbd oil

lsd blotters for sale

mescaline powder

cocaine online

anabol 5mg

nembutal powder

pill online

quagen online




abilify 10 mg

british dispensary

sweeter high

gold luxury

subutex 8mg

nicotine lozenges

pills online

one up micro dose

buy pill online

one up micro dose

glock43 gen 5

carts shop said...

Glock store

Glocks for sale

glock43 gen4 mos

glock 17 gen4

glock 17 gen5

glock 21 For Sale

glock 26

glock 17x

glock 42

glock 19 gen 5

glock 19 Mos Gen 5

glock 45 gen 4

glock 19 gen-3

magazine usa gen4

glock 43 For Sale

glock 34 gen 5


david anderson said...

For getting instant Assignment Help, you can check our website and order assistance. Assignments are written on a priority basis and delivered to you without any delay. If you choose our writing services then you will have thousands of other advantages as well.

Maha Nazir said...

The service that Imarah Marketing offers is the best out there. There are no hidden fees and we were in complete contact throughout the entire process. I We would definitely recommend Imarah real estate Marketing to others looking to buy a property in Islamabad Pakistan

Unknown said...

In all our years of buying property we never had the pleasure of working with a Imarah real estate marketing professional as thorough and reliable as them!

ExpressHealth said...


Maha Nazir said...

Imarah Marketing was awesome. Can’t say enough good things about them. Our agent helped us navigate the process in a smooth way and helped us sort out any issues that we faced. https://imarahmarketing.com/

Dreamland marketing said...

Blue World City Islamabad is a project of Blue Group of Companies Owned by Saad Nazir, Son of ex-Deputy Commissioner Lahore Ch. Nazeer.
The developers of BWC are set to develop this project on more than 60000 Kanals which has enough room to create access from all major roads of Rawalpindi and Islamabad in future.
For more visit: https://dreamlandmkt.com/blue-world-city-islamabad/

Dreamland marketing said...

Capital Smart City Islamabad is a joint project between Future Developments Holdings and Habib Rafiq (Pvt). Habib Rafiq Pvt. is a well-known company in the Real estate industry of Pakistan.
The Capital Smart City Islamabad (CSCI) is located in the Rawalpindi Region of Islamabad, near the new Islamabad International Airport. The society is located at Lahore-Islamabad Motorway at a distance of 9.2 km from M-2 Toll Plaza near the Thalian interchange. Located on the eastern route of CPEC, it is only 5-6 minutes away from the new Islamabad airport.
For more visit: https://dreamlandmkt.com/capital-smart-city-islamabad/

Dreamland marketing said...

Nova city Islamabad is the project of very well-known and famous real estate developers Nova city developers. Nova city housing project is the premium project of nova city developer and they are known for their high quality infrastructure style.
Nova City Islamabad is located in the prime location of Islamabad and Rawalpindi. The society’s main entrance has direct access to Ring Road Rawalpindi and other side along with the CPEC route. The society’s location is the central place because it offers easy accessible to Islamabad and Rawalpindi to its residents.
For more visit: https://dreamlandmkt.com/nova-city-islamabad/

Dreamland marketing said...

Park View City is a housing society located in the heart of Margalla Hills.
The society is directly connected with the current Kurri Road through Malot Road. This gives the society direct access from Islamabad’s park road and makes it an easy 15 minutes drive to Islamabad Blue Area. Also, from the eastern end, the Simli Dam Road connects with Malot Road that gives the society easy access to BharaKahu and beyond.
For more visit: https://dreamlandmkt.com/park-view-city-islamabad/

Dreamland marketing said...

Dreamland Marketing is one of the Leading Real Estate marketing companies in Pakistan with headquarter located in F11 Markaz, Islamabad. With various services ranging from sales to project marketing, the company has seen high growth in recent years. The company helps buyers, owners, and investors understand their real estate needs and gives them the right advice about property buying and investment.
Dreamland has a team of qualified professionals who are friendly and provide professional support to our clients. The company aims to make an environment congenial to the professional growth of the employees.
The group specializes in providing the best real estate solutions to its clients and focuses on its services as per the needs of potential buyers.
For more visit: https://dreamlandmkt.com/

Imarah Real Estate Marketing said...


amna said...

Many thanks for your kind and efficient service for booking a plot in park view city and blue world city.I will definitely recommend Imarah real estate services to others in the future.

amna said...

Amazing and Helpful post. I really appreciated your work.Many thanks for your kind and efficient service for booking a plot in park view city and blue world city.I will definitely recommend Imarah real estate services to others in the future.

The Masters Real Estate said...

The Masters Real Estate is one of the most enticing options on the planet of real estate. Our prestigious forum provides the potential solution of all your ambiguities regarding the purchasing of shops, lands, and houses. We have engaged our teams in various cities of Pakistan as well as the online world to make our services accessible. The provision of reasonable price real estate properties by our company is attracting the residents and the foreigners. We have intended to provide luxurious construction projects and houses at affordable prices to our nation.
For More Visit: Lahore Smart City

Umair said...


Microsoft 365 said...

Microsoft365.com/setup: Go to Microsoft365.com/setup, Microsoft 365 Setup website. Sign In to your Office Account or Create a New One. Now Enter Product Key. Click Install Office Button. Go ahead and Click Install Office Again. Save and Run office setup. Wait for Office to finish Installation. Click Close, Installation is finished.

Najaf Qureshi said...

packaging host
Cheap Latest Products

Haris Legend said...

These are some of the main issues I’ve found there and because of it I decided to start my own taxi/ chauffeur company in London Heathrow Taxi London Chauffeurs Here's you can find taxi services 24/7.

Haris Legend said...

You did an excellent job on this post! I’d like to add an extra productivity tip to your list if you don’t mind? I decided to start my specialist review blog with the brand name “Bow Advisor”. Best compound bow for beginners Best crossbow for Women Here's you can review the latest articles on this niche.

Lazaro Coley said...

This is good advice for those new to the world of blogging. Thank you very much for sharing this brief but accurate information.
You can visit my latest posts related to Game Coaching Services for Beginners. I appreciate our visit video game coaching. Thanks for your concern.