Screen Video Record of Current Activity Android

Screen Video Record of Current Activity Android

EDIT: This answer is superceded by the answer below from Danpe.

Programmatically recording video from within your app will require root access. You'll notice that the apps available to do this in the Play Store prominently list "REQUIRES ROOT" in their app descriptions. You'll also notice that there may also be some specific hardware requirements for this approach to work ("Does not work on Galaxy Nexus or Tegra 2/3..." -- from the description of the Screencast Video Recorder app.

I have never written this code myself, but I've put together a very high level idea of the approach required. It appears from this post that you have to access the frame buffer data via "/dev/graphics/fb0". The access mode for the frame buffer is 660, which means that you need root access to get to it. Once you have root access, you can use the frame buffer data to create screen shots (this project might work for this task) and then create video from these screenshots (see this other SO question on how to create video from an image sequence).

I've used the Screencast app and it works well on a Samsung Note. I suspect that this is the basic approach they've taken.

how to record video of current screen activity programmatically in android

There are no APIs yet in Android to do this directly. However, you could take screenshots of the app, and then merge them to make a video out of the images.

However, this process is quite tedious, and would probably take a lot of research and investigation.

Since you know how to take screenshots, a good starting point would be to look for libraries/APIs that can take in a series of images, and convert them into a video.

ffmpeg/JMF could be two good starting points.

Screen video capture of the specific views

Great that we can convert views to images and then just use it for a record (blending with other image - frame from camera for example)

public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}

So I can just convert some RelativeLayout (which contains elements I want to record to a video) to image

How can i record the Android device screen?

the only way to do that is with a rooted device.

try reading this answer: Programmatic screencapture on mobile device

EDIT 1:

screen capture as video is the same is screen capture of only one image.

EDIT 2:

there are different apps that record video from screen such as Screencast and ShootMe. you can call them from within the app using the startActivity(new Intent(String action)); with their action names.

for more read this: Open third party app

How to programmatically stop a screen video recorder from recording an Android app

By adding FLAG_SECURE into your Activity You can secure screen capturing functionality

getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE); 

write above setContenView();

from documentation

Android 5.0 lets you add screen capturing and screen sharing
capabilities to your app with the new android.media.projection APIs.
This functionality is useful, for example, if you want to enable
screen sharing in a video conferencing app.

The new createVirtualDisplay() method allows your app 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 API only
allows capturing non-secure screen content, and not system audio. To
begin screen capturing, your app must first request the user’s
permission by launching a screen capture dialog using an Intent
obtained through the createScreenCaptureIntent() method.

and Also from this documentation says

Window flag: treat the content of the window as secure, preventing it
from appearing in screenshots or from being viewed on non-secure
displays
.

NB
If you are using SurefaceView with media player then use SurfaceView.setSecure(true), then your video will be secured from any other apps.

How to take a screenshot of a current Activity and then share it?

This is how I captured the screen and shared it.

First, get root view from current activity:

View rootView = getWindow().getDecorView().findViewById(android.R.id.content);

Second, capture the root view:

 public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}

Third, store the Bitmap into the SDCard:

public static void store(Bitmap bm, String fileName){
final static String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
File dir = new File(dirPath);
if(!dir.exists())
dir.mkdirs();
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}

At last, share the screenshot of current Activity:

private void shareImage(File file){
Uri uri = Uri.fromFile(file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");

intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
try {
startActivity(Intent.createChooser(intent, "Share Screenshot"));
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No App Available", Toast.LENGTH_SHORT).show();
}
}

I hope you will be inspired by my codes.

UPDATE:

Add below permissions into your AndroidManifest.xml:

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

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.



Related Topics



Leave a reply



Submit