I need to take a screenshot of Activity
(without the title bar, and the user should NOT see that a screenshot has actually been taken) and then share it via an action menu button "share". I have already tried some solutions, but they didn't work for me. Any ideas?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I couldn't get Silent Knight's answer to work to work until I added
to my
AndroidManifest.xml
.This is what I use to take a screenshot. The solutions described above work fine for API < 24, but for API 24 and greater another solution is needed. I've tested this method on API 15, 24, & 27.
I placed the following methods in MainActivity.java:
I placed the following permissions and provider in my AndroidManifest.xml:
I created a file called provider_paths.xml (please see below) to direct the FileProvider where to save the screenshot. The file contains a simple tag that points to the root of the external directory.
The File was placed in the resource folder res/xml (if you don't have an xml folder here, just create one and place your file there).
provider_paths.xml:
This is how I captured the screen and shared it.
First, get root view from current activity:
Second, capture the root view:
Third, store the
Bitmap
into the SDCard:At last, share the screenshot of current
Activity
:I hope you will be inspired by my codes.
UPDATE:
Add below permissions into your
AndroidManifest.xml
:Because it creates and accesses files in external storage.
UPDATE:
Starting from Android 7.0 Nougat sharing file links are forbiden. To deal with this you have to implement FileProvider and share "content://" uri not "file://" uri.
Here is a good description how to do it.
This is how I captured the screen and shared it. Take a look if you are interested.
And the method that saves the bitmap image to external storage:
see more in : https://www.youtube.com/watch?v=LRCRNvzamwY&feature=youtu.be
If someone is getting an java.io.FileNotFoundException, following SilengKnight's solution, the problem is probably that writing in storage was not permited (Although we added the user-permission in the manifest). I solved it by adding this in the store function before making a new FileOutputStream.