Android Code Snippet A general solution to blocking screen capture (snapshot)

JackKirk

Well-Known Member
Licensed User
I decided to have another go at finding a general solution to this.

By "general" I mean the ability to turn snapshot blocking on and off at will when the code in the Activity module is running.

If you search the forums for "snapshot" and "screen capture" there are pretty limited suggestions as to how to block it - all solutions basically end up at:

https://www.b4x.com/android/forum/threads/disable-screen-capture-functionality.35753/#content (last post)

which judging by the post dates is referencing:

https://www.b4x.com/android/forum/threads/how-to-block-ban-screen-shots-of-app.52075/#post-326475(post #6 by NJDude)

I reproduce NJDude's code here:
B4X:
#If Java
import android.annotation.TargetApi;
import android.content.Context;
import android.view.WindowManager.*;
public void _onCreate() {
    this.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
}
#End If
Put this code in your Activity module and it will automatically run at Activity_Create - no calls required.

This works but is making the implicit assumption that you want your activity to never be able to be snapshot.

Unfortunately my circumstance is that I only want to prevent snapshots until the user completes a registration process (its a long story).

After considerable mucking around with this code and googling (my substitute for any real knowledge or understanding of Android and java) I managed to come up with:
B4X:
'This code can be anywhere in the Activity module:

..........

'Block screenshots
Private wrk_jo As JavaObject
wrk_jo.InitializeContext
wrk_jo.RunMethod("securescreen", Null)

..........

'Allow screenshots
Private wrk_jo As JavaObject
wrk_jo.InitializeContext
wrk_jo.RunMethod("unsecurescreen", Null)

..........

#If Java
import android.annotation.TargetApi;
import android.content.Context;
import android.view.WindowManager.*;
public void securescreen() {
    this.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
}
public void unsecurescreen() {
    this.getWindow().clearFlags(LayoutParams.FLAG_SECURE);
}
#End If
This is not restricted to Activity_Create - you can turn it on and off wherever you like.

I have incorporated this in my app and tested it on:

Android--SDK
6.0.1--23
7.0--24
10.0--29
11--30

I'd be interested in someone giving it a test on the missing Android/SDK levels and reporting back.

Happy coding...
 
Last edited:

Sandman

Well-Known Member
Licensed User
Slightly off-topic, sorry, but I'm curious - why is it important to block the screen capture?
 

Sandman

Well-Known Member
Licensed User
The only item in that list that might be actually relevant is the copyrighted images. The rest can easily be solved by taking a picture with another camera. Just saying, blocking isn't a perfect solution unless you really want to stop pixel-perfect images. (Which I imagine you could still make by using a rooted device or something like that.)
 

JackKirk

Well-Known Member
Licensed User
Slightly off-topic, sorry, but I'm curious - why is it important to block the screen capture?
Not off-topic at all.

In my case the user is given a series of thumbnail "previews" of potential content (containing him/her) of them swinging through trees.

They pay for a ticket for access but the ticket is not "consumed" until the first thumbnail is tapped causing the full image to be downloaded.

Before the ticket is consumed I want to minimize the "leakage" potential of snapshots of the thumbnails.

After the ticket is consumed I don't care.

Obviously they could take a photo of the screen - but this is further reducing the quality - this app feeds content to whatever social media platforms the user has on his device - quality will be important.

I also check for rooted devices - probably not the apps strong suit though.
 
Last edited:

aeric

Well-Known Member
Licensed User
How about adding watermark? If I understand what you are trying to do.
 

JackKirk

Well-Known Member
Licensed User
How about adding watermark?
I do that too - the project uses "AI" based facial recognition - one of the by products is I get to know the location and size of all faces in each photo - I put the watermark in the corner that is least crowded - works a treat.
 
Last edited:

aeric

Well-Known Member
Licensed User
I do that too - the project uses "AI" based facial recognition - one of the by products is I get to know the location and size of all faces in each photo - I put the watermark in the corner that is least crowded - works a treat.
Tile your trademark logo all around the photo but I learn that some graphic editing software is able to remove it.
 

JackKirk

Well-Known Member
Licensed User
Tile your trademark logo all around the photo but I learn that some graphic editing software is able to remove it.
Tiling would be overkill.

I know that some software can supposedly remove trademarks but in this case I suspect there is little reason to - the users are basically saying to their social media followers "look where I've been".
 
Top