I am being able to take snapshot of Hello World app through below code.'
private void takeScreenshot()
``{
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
I have launched another app (excel) using Intent (getLaunchIntent for package method). Now I want to take screenshot of launched app. I tried to call the same method as above and changed the View as below:
View v1 = findViewByID(android.R.id.content);
But when I try to retrieve Drawing Cache through
Bitmap cac = v1.getDrawingCache()
I am getting cac as NULL. How can I retrieve the view of launched app so that getDrawingCache does not return NULL?
How can I retrieve the view of launched app
You can't, for obvious privacy and security reasons.
You are welcome to use the media projection APIs to take screenshots on Android 5.0+, with user permission.
Related
I read nearly every answer here. Is there any way to capture a screenshot from a background service, which will return a bitmap?
I know I can do a screenshot with AccessibilityService and/or MediaProjectionApi with an invisible Activity. But in every solution the image is saved (at least temporary) on the device. Which I prefer to avoid.
Check this out, you got your bitmap.
/** Take Screen Shot */
private void takeScreenshot() {
try {
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + uniqueID + "_" + getEventTimeLocal() + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
//// then we can delete your file
deleteScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or DOM
Log.i(TAG, "exception': " + e.printStackTrace());
}
}
I use this code in a service class to take screenshot in the service but getWindow() is not available here. what should i do?
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
}
}
Try this approach:
Pass a reference to the activity when you create the class, and when calling relevant methods and use it. But there can be memory leaks with this approach
void MethodThatUsesActivity(Activity myActivityReference) {
myActivityReference.getWindow().getDecorView().getRootView();
}
Hello everyone I will try to be as clear as possible .... I have a method in which I get the Screen of my App, what I would really like is to be able to use my method to get Screen from other apps, or the Android desktop, The way I try to do it is before I capture the screen, I transform my Layout into INVISIBLE, but my capture goes black, if I capture my app if it comes out perfect, but I want to capture other apps, any ideas? ... I show you my method ..
public void addListenerOnButton4() {
Button btnTakeScreenshot = (Button) findViewById(R.id.share);
btnTakeScreenshot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takeScreenshot();
}
});
}
public void takeScreenshot() {
RelativeLayout ln = (RelativeLayout) findViewById(R.id.Layout);
ln.setBackgroundColor(Color.TRANSPARENT);
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = (Environment
.getExternalStoragePublicDirectory(Environment
.DIRECTORY_DOWNLOADS) +File.separator+now+"ScreenShoot.jpg");
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
- API level 21+
You can use MediaProjectionManager. Details here.
MediaProjection implementation gives you a Bitmap that you can save as JPEG. After you get byteArray just write it to the file as you usually do in Java.
Bitmap bmp = // your Bitmap here
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
- API before 21
Android gives a way to capture only View but when you make it INVISIBLE there's no color for you View anymore. That is why you're getting the black screen.
You can't take a screenshot of other apps just making your View invisible because the capture function connected to the View.
- Any API + root
If you want to do this anyway you need root access. Then you can read framebuffer and get the raw data and convert it into Bitmap. Check details here.
I can not get view using getWindow inside service.
I used this view for any screen outside application.
View v1 = getWindow().getDecorView().getRootView();
How can I capture screenshot inside service?
or how can I get view for any screen outside application?
Punch Line : You are very bad in asking questions.
But I will try to answers the questions that I understood.
How do I take Screenshots?
private void getScreenshot() {
try {
String imagePath = Environment.getExternalStorageDirectory().toString() + "/" + /*NAME*/ + ".jpg";
View view = getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
File imageFile = new File(imagePath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
Do what you want to do with the screenshot now.
Also I did not understand what you wanted to to ask with,
View v1 = getWindow().getDecorView().getRootView();
You can clarify that further if you want.
I am trying to use the VuDroid PDF viewer and I need to take the rendered bitmap and store it as a byte[]. Then I need to convert it back into a Bitmap that can be displayed on a view using something like "canvas.drawBitmap(bitmap, 0, 0, paint);".
I have spent many hours trying to access the Bitmap and I might have done it already, but even if I get the byte[] to return something it still wont render as a Bitmap on the canvas.
Could someone please help me here, I must be missing something. Thank you so much.
I believe it is supposed to accessed via...
PDFPage.java .... public Bitmap renderBitmap(int width, int height, RectF pageSliceBounds)
-or-
through Page.java -or- DocumentView.java -or- DecodeService.java
Like I said I have tried all of these and have gotten results I just cannot see where I am going wrong since I cannot render it to see if the Bitmap was called correctly.
Thank you again :)
The doc says the method returns "null if the image could not be decode." You can try:
byte[] image = services.getImageBuffer(1024, 600);
InputStream is = new ByteArrayInputStream(image);
Bitmap bmp = BitmapFactory.decodeStream(is);
I think This will help you:-
Render a byte[] as Bitmap in Android
How does Bitmap.Save(Stream, ImageFormat) format the data?
Copy image with alpha channel to clipboard with custom background color?
if you want to get each pdf page as independent bitmap you should consider that
VuDroid render the pages,
PDFView only display them.
you should use VuDroid functions.
now you can use this example and create your own codes
Example code : for make bitmap from a specific PDF page
view = (ImageView)findViewById(R.id.imageView1);
pdf_conext = new PdfContext();
PdfDocument d = pdf_conext.openDocument(Environment.getExternalStorageDirectory() + "your PDF path");
PdfPage vuPage = d.getPage(1); // choose your page number
RectF rf = new RectF();
rf.bottom = rf.right = (float)1.0;
Bitmap bitmap = vuPage.renderBitmap(60, 60, rf); //define width and height of bitmap
view.setImageBitmap(bitmap);
for writing this bitmap on SDCARD :
try {
File mediaImage = new File(Environment.getExternalStorageDirectory().toString() + "your path for save thumbnail images ");
FileOutputStream out = new FileOutputStream(mediaImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
for retrieve saved image:
File file = new File(Environment.getExternalStorageDirectory().toString()+ "your path for save thumbnail images ");
String path = file.getAbsolutePath();
if (path != null){
view = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path), YOUR_X, YOUR_Y, false);
}
Try this code to check whether bitmap is properly generating or not
PdfContext pdf_conext = new PdfContext();
PdfDocument d = (PdfDocument) pdf_conext.openDocument(pdfPath);
PdfPage vuPage = (PdfPage) d.getPage(0);
RectF rf = new RectF();
Bitmap bitmap = vuPage.renderBitmap(1000,600, rf);
File dir1 = new File (root.getAbsolutePath() + "/IMAGES");
dir1.mkdirs();
String fname = "Image-"+ 2 +".jpg";
File file = new File (dir1, fname);
if (file.exists ())
file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}