Screenshot from background service of another application (programmatically) - android

When I use the browser I want to save screenshots of the site that I visited. Because some pages disappear in the future. So I decided to do a background service that would make the screenshots at regular intervals of time when I visit the site say www.site.com. Who can give me any tips, links to tutorials, examples, ...?
P.S. My phone is rooted. Android 2.1. and do not say that it is impossible :)
UPDATE:
Screenshots in JPG format or HTML without a difference. The method which is easier to make.

Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
then read img.png as bitmap and convert it jpg as follows
Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+ File.separator +"img.png");
//my code for saving
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.JPEG, 15, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();

https://market.android.com/details?id=com.edwardkim.android.screenshotitfullnoroot&hl=en
Doesn't need to be rooted.
http://maketecheasier.com/take-screenshots-on-android-phone/2010/07/16
Would need to be rooted.
Worst case scenario you can use android SDK while plugged in via USB and take screen shots.

Related

android Environment.getExternalStorageDirectory().getPath()

I have this code:
try {
URL url = new URL (strURL);
input = url.openStream();
byte[] buffer = new byte[1500];
OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");
and I get this error:
do not hardcode /sd card/ use environment.getexternalstoragedirectory().getpath() instead
in
OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");
I have read about that:Android 4.2 - Environment.getExternalStorageDirectory().getPath() behaviour
So the question is what will be final code when I replace it?
sd folder name different in Some phones
In samsung phones, it is named external_sd , and your code will fail.
control+shift+o --> to add imports in eclipse,see this link
"/sdcard/" is replace with "Environment.getExternalStorageDirectory().getPath()" in your code
The problem was that you call a file called Environment.java itself, so Eclipse didn't give me the choice to import Environment change that one.
Your revised code should look like
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+pos+".png");
You should be careful as not all devices have a /sdcard/ path.
Devices across the years have changed and this may not always contain a link to the proper external storage directory.

How can I open raw image files?

In my application , I am able to read framebuffer data but I cant open the image file ,I dont know why but what I came to know by searching is it is in the raw format .
Can someone tell me how can open this raw format of data on to a human readable image format
My code to get raw image file is as follows
Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + ss.getAbsolutePath()).getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
Thanks in advance
If i get your question than try this:-
myIageView.setImageResource(R.raw.imageName);

Android share display on another devices

i'm working on share the tablet display with more than one table (all rooted) connected through WiFi , i'm using the following approach (all inside one thread) :
1- i take a screen shot.
Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -P " + "/sdcard/test/img.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
2- compress the screen shot image.
Bitmap mBitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + "/test/img.png");
OutputStream outputStream = null;
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/test/img2.png");
outputStream = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 15, outputStream);
outputStream.flush();
outputStream.close();
3- open socket and send the compressed image to another tablet.
this is working but my problem is the viewing delay in the other tablet it took 4-5 sec to refresh the new display , is there any better approach to have it real time display?
Unfotrtunately, this function will take long time. It is linked with process lifecycle, IPC and the slow file system. You need to have a look at this library or the source code of /system/bin/screenshot util. You have to reuse native(c-language) functions from sources, and it is not a trivial task.

Android: The ways to change build.prop

I need to build an application which enables me to edit the build.prop by using my own application. This is to optimize some of the values in the build.prop. Of course, I can edit this file using a file manager with root access. But my problem is, I need to edit this using an application that I create, and make it easy for the novice users to edit the file with recommended settings by my application. Please help me figure out this. Thanks for you time!
Something like this should work. Code hasn't been tested and might need some changes
Requesting Superuser Access
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("mount -o remount rw /system/\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
File file=new File("/system/build.prop");
Read file
FileInputStream fis = openFileInput("/system/build.prop");
String content = "";
byte[] input = new byte[fis.available()];
while (fis.read(input) != -1) {}
content += new String(input);
EDIT String content here.
Write file
DataOutputStream outstream= new DataOutputStream(new FileOutputStream(file,false));
String body = content;
outstream.write(body.getBytes());
outstream.close();
Remember to backup your build.prop in-case something goes wrong during editing.

Unable to take screenshots

I have this code to take screenshots of layouts in Android. It is not throwing any errors, however, the screenshot is not being taken either. Can someone please help me figure out what I am doing wrong here? I am new with Eclipse and I am having a hard time figuring things out. Also if there is any other way to take screenshots can you post it as an answer to this thread? Thanks for your time!
private void getScreenshot()
{
View content = findViewById(R.id.testView);
content.setDrawingCacheEnabled(true);
content.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());
content.setDrawingCacheEnabled(false);
File file = new File( Environment.getExternalStorageDirectory() + "image.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
Do you need to add path separator into your File? i.e.
File file = new File(Environment.getExternalStorageDirectory() +
File.separator + "image.png");
You should add a lot more logs and tests in your code to check whether it is behaving as you would expect, e.g.
Log the details of the file you are trying to create to be sure it is correct.
Once you've created the file, test that it exists, e.g. if (!file.exists())
The Bitmap.compress function returns a boolean, so you should check the return value and log it to see if it succeeded.
One other thought: maybe you need to call ostream.flush() (API docs here) to ensure the buffered data is written to the file?
I'm assuming you're writing this code for use within your app. You probably already know this, but DDMS provides a way to take screenshots in case you just want to take some yourself. Just make sure to select the device to enable the Screenshot menu option.

Categories

Resources