Android -- taking a screen shot - android

I need to take a screen shot and save the screen shot. I need to do this without using any connection to PC or without unrooting the phone. I need to do this whenever a event is triggered . For example when an ad is shown in a game ... or when the game ends and shows the scores in snake etc. Can you please let me know How Can i do this. I saw some tutorilas and they gave the code but that doesnt seem to work
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();
}
}

You have to enable the cache first, before calling getDrawingCache().

View ve = findViewById(R.id.loyout);
ve.setDrawingCacheEnabled(true);
Bitmap b = ve.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString()
+ "/SaveCapture";
myPath = new File(extr);
if (!myPath.exists()) {
boolean result = myPath.mkdir();
Toast.makeText(this, result + "", Toast.LENGTH_SHORT).show();
}
myPath = new File(extr, getString(R.string.app_name) + ".jpg");
Toast.makeText(this, myPath.toString(), Toast.LENGTH_SHORT).show();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b,
"Screen", "screen");
}
catch (FileNotFoundException e) {
Log.e("Error", e + "");
}
catch (Exception e) {
Log.e("Error", e + "");
}

Can you give more information as to what does not work when you run that code ? Does it not capture what you want ? Does it crash ?
Make sure you change the R.id.layoutroot correctly with your root layout... Beside that it seems like it would work...
<com.example.android.snake.SnakeView
android:id="#+id/snake"
android:layout_width="match_parent"
android:layout_height="match_parent"
tileSize="24"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/text"
android:text="#string/snake_layout_text_text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#ff8888ff"
android:textSize="24sp"/>
</RelativeLayout>
Edit...
So for example, if you use that layout you just put there, you should change the R.id.layout into R.id.snake, that's because of this line : android:id="#+id/snake".
I don't think there is an easy way to find /get the id of the "root" layout of a view (if you wanted to take screenshot of anything the phone is showing.
I just checked the launcher and another application, seems like most app are placed into a FrameLayout with id/content, so you can try to use android.R.id.content, but there is no guaranty this will work every time...

Related

Taking screenshot of a view gives black image?

I tried to take a screenshot of a imageview programatically! When the image is saved to folder it is producing black image! when i tried to take screenshot of my entire scroll view, I am able to take screenshot. Please help:
my Code is:
private void saveop(){
RelativeLayout parent = (RelativeLayout) findViewById(R.id.opimagelayout);// which includes your view or buttons?
Bitmap bitmap = Bitmap.createBitmap(384,
100, Bitmap.Config.ARGB_8888);
Canvas bitmapHolder = new Canvas(bitmap);
parent.draw(bitmapHolder);
try {
final File f = savebitmaps(bitmap,"operator","");
} catch (IOException e) {
e.printStackTrace();
}
}
public static File savebitmaps(Bitmap bmp,String transno, String currentDateandTime) throws IOException {
Bitmap out = BITMAP_RESIZER(bmp, 384,112);
File folder = new File(Environment.getExternalStorageDirectory() + "/MobeeLoad");
File file = new File(folder, transno+".jpg");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
out.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
bmp.recycle();
out.recycle();
} catch (Exception e) {
}
bmp.recycle();
return file;
}
Layout xml:
<RelativeLayout
android:id="#+id/opimagelayout"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_below="#+id/date"
android:layout_height="90dp">
<ImageView
android:id="#+id/opimage"
android:layout_width="90dp"
android:src="#drawable/check"
android:layout_centerHorizontal="true"
android:layout_height="90dp" />
</RelativeLayout>

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.

Capturing writing on the touch screen

My application has a requirement where the user can write their signature on the screen. The signature should be able to be extracted into an image.
How should I go about this ?
Is there an existing android process/observer for this ? Or are they any libraries / plugins that might allow the functionality ?
use this code in Activity:
try {
GestureOverlayView gestureView = (GestureOverlayView) findViewById(R.id.signaturePad);
gestureView.setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(gestureView.getDrawingCache());
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "signature.png");
f.createNewFile();
FileOutputStream os = new FileOutputStream(f);
os = new FileOutputStream(f);
//compress to specified format (PNG), quality - which is ignored for PNG, and out stream
bm.compress(Bitmap.CompressFormat.PNG, 100, os);
os.close();
} catch (Exception e) {
Log.v("Gestures", e.getMessage());
e.printStackTrace();
}
}
In xml:
<android.gesture.GestureOverlayView
android:id="#+id/signaturePad"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:background="#android:color/white"
android:eventsInterceptionEnabled="true"
android:fadeEnabled="false"
android:gestureColor="#android:color/black"
android:gestureStrokeLengthThreshold="0.1"
android:gestureStrokeType="multiple"
android:orientation="vertical"/>

How to take a screenshot of activity(game) programmatically

I am aware that not all Android devices can take screen shots, but I would like to be able to take screen shots of my users game screen programmatically so they can share it with friends etc. Kinda like Hill Climb racing does in there game.
Does anyone know how to do this? I tried a few solutions that are posted on here:
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;
// create bitmap screen capture
Bitmap bitmap;
View v1 = activity.getCurrentFocus().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But no luck. Any other way to do this? Maybe a new method.
EDIT:
I am getting a null pointer at line View v1 = activity.getCurrentFocus().getRootView();
Try This In this I take screen shot of current layout and save in sdcard...
To Capture Screen Shoot
Use
View v1 = getWindow().getDecorView().getRootView();
in place of
View v1 = activity.getCurrentFocus().getRootView();
it will not throws Null Pointer Exception.
Enjoy!!

Screenshot is black

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 :)

Categories

Resources