Screenshot is black - android

I'm trying to take a screen shot and save it as a png file on the sdcard. My file is saved with its file size as 1.57Kb but it is black. I'm using the following code:
View content = findViewById(R.id.id_ll_SurfaceView);
content.setDrawingCacheEnabled(true);
Bitmap b = content.getDrawingCache();
b.createBitmap(800, 480, Config.ARGB_8888);
File file = new File("/sdcard/test.png");
try
{
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
FileOutputStream(
Environment.getExternalStorageDirectory().getAbsoluteFile()+"/test.jpg"));
b.compress(CompressFormat.PNG, 100, fos);
fos.close();
Toast.makeText(getApplicationContext(), "Saved", 0).show();
}
catch (Exception e)
{
e.printStackTrace();
}

Are you taking screenshot from onCreate()..You have to wait till view is completely drawn on the screen..Use ViewTreeObserver to get a callback when view is completely drawn on the screen..
add this code in onCreate..
ViewTreeObserver vto = yourView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
yourScreenshotFunction();
}
});
Now implement this function
public void yourScreenshotFunction(){
//Add your screenshot taking code here..
}
See my question here..I have explained the procedure under title Final Outcome..

I am doing just,
main.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(main.getDrawingCache());
File file = new File("/sdcard/test.png");
try
{
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
screenshot.compress(CompressFormat.PNG, 100, fos);
fos.close();
Toast.makeText(getApplicationContext(), "Saved", 0).show();
}
catch (Exception e)
{
e.printStackTrace();
}
Here main is your activity's view..
EDIT: Look at this tutorial Android take screenshot from code

Go to eclipse...
Go to Windows
Open Perspective
DDMS
Select your emulator or phone on the left hand side.
Click on the camera icon just above in the toolbar.
EASY AS THAT :)

Related

Video section not captured while taking screenshot

I am working on a project where I wanted to take screenshots of the current window (inside the application only).
My application consist of a webview, and I am loading webpages on that. webpage contains videos, images...
If the webpage plays the video and I try to take the screenshot it shows as black color in the video player position.
Method to take screenshot (Source)
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
Used libraries
Screenshott
hiddenshot
InstaCapture
None of the above are worked.
Here is a sample screenshot taken by the device
Please let me know if there is any way to fix this issue.

to take screenshot and to draw on it (android)

I want to take screenshot of my WebView by clicking on menu item . And then I want to draw on this screenshot and save it on my sdcard.
I've also found the solution of taking screenshot and saving. Here it is:
private void getScreen(View content) {
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/screen.png");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and on onOptionsItemSelected() method:
View content = findViewById(R.id.webview_main);
content.setDrawingCacheEnabled(true);
getScreen(content);
It`s work ok, but I want to implement drawing on it too. If anyone know, how to do this, tell me please,
I will be very grateful.

How to take a screenshot of the current MapView?

I have two buttons and a mapView. I want to save the MapView's view when I press one of the buttons.
Anyone got any idea about how I can do this?
You have to add a listener to the button (you should know how this done) and in that listener, do something like that:
boolean enabled = mMapView.isDrawingCacheEnabled();
mMapView.setDrawingCacheEnabled(true);
Bitmap bm = mMapView.getDrawingCache();
/* now you've got the bitmap - go save it */
File path = Environment.getExternalStorageDirectory();
path = new File(path, "Pictures");
path.mkdirs(); // make sure the Pictures folder exists.
File file = new File(path, "filename.png");
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(file));
boolean success = bm.compress(CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
mMapView.setDrawingCacheEnabled(enabled); // reset to original value
In order to have the image show up in the Gallery immediately, you have to notify the MediaScanner about the new image like so:
if (success) {
MediaScannerClientProxy client = new MediaScannerClientProxy(file.getAbsolutePath(), "image/png");
MediaScannerConnection msc = new MediaScannerConnection(this, client);
client.mConnection = msc;
msc.connect();
}
google map have a function to take a snapshot so use this here is example, it return a bitmap that you can set on a image view its simple..
Bitmap bitmapMapsattelite ;
map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
#Override
public void onMapLoaded() {
// Make a snapshot when map's done loading
map.snapshot(new GoogleMap.SnapshotReadyCallback() {
#Override
public void onSnapshotReady(Bitmap bitmap) {
bitmapMapsattelite = null;
/// make the bitmap null so everytime it //will fetch the new bitmap
bitmapMapsattelite = bitmap;
}
});
}
});
I used in this way to take only fragment screen using OSMDroid.
private void captureScreen() {
// put captureScreen() inside your OSMDroid fragment
// it's better put a button inside your layout OSMDroid fragment.xml
view = view.findViewById(R.id.mapview); //using OSMDroid fragment to call <org.osmdroid.views.MapView/>
view.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());
//build your folder
File sd = new File(Environment.getExternalStorageDirectory() + "/ArqueoMaquina");
if (!sd.exists()) {
sd.mkdir();
}
File imagePath = new File(sd , "mapa" + System.currentTimeMillis() + ".png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Uri uri = Uri.fromFile(imagePath); //to take the file path to your .png map saved in your phone
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
Those are the results. My screen App with OSMDroid fragment and Button "Save Map":
And my map.png saved in folder phone showing only the OSMDroid fragment:
The required functionality was introduced in Osmdroid 6.1.0. The corresponding class is MapSnapshot.
For the use case in the question adding the below code in the click callback of the button would work.
final MapSnapshot mapSnapshot = new MapSnapshot(new MapSnapshot.MapSnapshotable() {
#Override
public void callback(final MapSnapshot pMapSnapshot) {
if (pMapSnapshot.getStatus() != MapSnapshot.Status.CANVAS_OK) {
return;
}
final Bitmap bitmap = Bitmap.createBitmap(pMapSnapshot.getBitmap());
// Do something with the bitmap like save to file
}
}, MapSnapshot.INCLUDE_FLAG_UPTODATE, pMapView);
new Thread(mapSnapshot).start();
Reference : https://github.com/osmdroid/osmdroid/issues/1737
Google map has a call back named snapshot -> map.snapshot that return a Bitmap to handling.
In osmdroid (open street map android api), corresponding method is getDrawingCache method -> map.getDrawingCache

Screenshot of a webpage with Flash elements

I would like to take a screenshot of a page displayed in a WebView. The page contains flash elements - and here is the problem. When I take a screenshot all the flash parts of the page are blank.
I am using this piece of code to take a screenshot:
WebView webView = (WebView) findViewById(R.id.webview);
Picture picture = webView.capturePicture();
Bitmap screenshot = Bitmap.createBitmap(picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(screenshot);
picture.draw(c);
File file = new File("/sdcard/screenshot.jpg");
FileOutputStream ostream = null;
try {
file.createNewFile();
ostream = new FileOutputStream(file);
screenshot.compress(CompressFormat.JPEG, 90, ostream);
Log.d("Test", "Screenshot taken");
} catch (Exception e) {
Log.e("Test", "Error", e);
} finally {
try {
ostream.close();
} catch (IOException ignore) {
}
}
I was also trying to acquire Bitmap of the screen contents using the solution given in this SO question:
View webView = findViewById(R.id.webview);
Bitmap bitmap = webView.getDrawingCache();
This also doesn't work - the result is the same (i.e. blank flash elements).
So the question is: How to take a screenshot of WebView contents also with flash elements?

screenshot but creates blank file : android?

I am working on android. I have following code to capture a screenshot.
Setting up your Root layout:
View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);
Function to get the rendered view:
private void getScreen()
{
View content = findViewById(R.id.layoutroot);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/test.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
My problem is that it creates a zero kb file. what could be problem?
do these changes and try again:
make the content variable a final and call findViewById on the layout of the activity
final View content = somelayout.findViewById(R.id.layoutroot);
You have not write data to the file. you are directly closing the file thats why you got a file with 0 kb
use something like this
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
byte[] mydata = null;//data in byte array
ostream.write(mydata);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}

Categories

Resources