Video section not captured while taking screenshot - android

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.

Related

Why is image corrupted after 2-3 times byte array to image conversion?

I am writing bytes to jpg image, but after writing bytes, whenever I am trying to open that image from ES file explorer, image gets corrupted.
Below is the code I am trying:
private void overWriteImage() {
if (file.exists()) {
file.delete();
}
try {
FileOutputStream fos = new FileOutputStream(file.getPath());
fos.write(this.imageBytes);
fos.close();
} catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
}

Black portion in place of transparent portion while saving the bitmap

I am frustrated searching almost every google page for this problem.
I need to save my bitmap to file.
I have used this method several times with no problem at all. But now I am having a problem.
The Bitmap I am saving is a round image with transparency and having .png format which I have placed in res folder. But I am getting black portion in place of transparent portion.
This is the code I am using for saving the bitmap.
void saveImage(Bitmap bmp) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bytes);
finalImg = new File(Environment.getExternalStorageDirectory(),
"emoticon_temp" + ".png");
finalImg.createNewFile();
FileOutputStream fo = new FileOutputStream(finalImg);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am saving this bitmap as file because I need to share this image and for sharing the image I need the Stream Uri.
If you got any other Idea for sharing a bitmap, please let me know.

Image to pdf conversion. pdf file is chopped for Nexus s & above resolution/size device :-

I am taking snap shot of current screen storing it in sd card and then converting it to pdf format. All is well if i test it for Nexus one but as i go for higher resolution devices (Nexus s,Galaxy Nexus) .The converted pdf is not showing full screen shot rather it is chopped . I have shared below the code used by me .Thanx in advance .
My Method for taking screen shot :
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.JPEG,100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
My method for pdf conversion :-
void Pdfconverter() throws Exception
{
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("mnt/sdcard/FirstPdf.pdf"));
document.open();
Image image = Image.getInstance (Environment.getExternalStorageDirectory() + "/screenshot.png");
document.add(new Paragraph("Heading for the Image"));
document.add(image);
document.close();
}

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

Android/Java: Saving a byte array to a file (.jpeg)

I am developing an application for Android, and part of the application has to takes pictures and save them to the SDcard. The onPictureTaken method returned a byte array with the data of the captured image.
All I need to do is save the byte array into a .jpeg image file. I have attempted to do this with the help of BitmapFactory.decodeByteArray (to get a Bitmap) and then bImage.compress (to an OutputStream), a plain OutputStream, and a BufferedOutputStream. All three of these methods seem to give me the same weird bug. My Android phone (8MP camera and a decent processor), seems to save the photo (size looks correct), but in a corrupted way (the image is sliced and each slice is shifted; or I just get almost horizontal lines of various colors); and The weird thing is, that an Android tablet with a 5MP camera and a fast processor, seems to save the image correctly.
So I thought maybe the processor can't keep up with saving large images, because I got OutOfMemory Exceptions after about 3 pictures (even at compression quality of 40). But then how does the built in Camera app do it, and much faster too? I'm pretty sure (from debug) that the OutputStream writes all the data (bytes) and it should be fine, but it's still corrupted.
***In short, what is the best/fastest way (that works) to save a byte array to a jpeg file?
Thanks in advance,
Mark
code I've tried (and some other slight variations):
try {
Bitmap image = BitmapFactory.decodeByteArray(args, 0, args.length);
OutputStream fOut = new FileOutputStream(externalStorageFile);
long time = System.currentTimeMillis();
image.compress(Bitmap.CompressFormat.JPEG,
jpegQuality, fOut);
System.out.println(System.currentTimeMillis() - time);
fOut.flush();
fOut.close();
} catch (Exception e) {
}
and
try {
externalStorageFile.createNewFile();
FileOutputStream fos = new FileOutputStream(externalStorageFile);
fos.write(args);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
All I need to do is save the byte array into a .jpeg image file.
Just write it out to a file. It already is in JPEG format. Here is a sample application demonstrating this. Here is the key piece of code:
class SavePhotoTask extends AsyncTask<byte[], String, String> {
#Override
protected String doInBackground(byte[]... jpeg) {
File photo=new File(Environment.getExternalStorageDirectory(), "photo.jpg");
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos=new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
}
catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
return(null);
}
}
Hey this codes for kotlin
camera.addCameraListener(object : CameraListener(){
override fun onPictureTaken(result: PictureResult) {
val jpeg = result.data //result.data is a ByteArray!
val photo = File(Environment.getExternalStorageDirectory(), "/DCIM/androidify.jpg");
if (photo.exists()) {
photo.delete();
}
try {
val fos = FileOutputStream(photo.getPath() );
fos.write(jpeg);
fos.close();
}
catch (e: IOException) {
Log.e("PictureDemo", "Exception in photoCallback", e)
}
}
})
This Code is perfect for saving image in storage, from byte[]...
note that "image" here is byte[]....taken as "byte[] image" as a parameter into a function.
File photo=new File(Environment.getExternalStorageDirectory(), "photo.jpg");
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos=new FileOutputStream(photo.getPath());
Toast.makeText(this, photo.getPath(), Toast.LENGTH_SHORT).show();
fos.write(image);
fos.close();
}
catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
}
Here's the function to convert byte[] into image.jpg
public void SavePhotoTask(byte [] jpeg){
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Life Lapse");
imagesFolder.mkdirs();
final File photo= new File(imagesFolder, "name.jpg");
try
{
FileOutputStream fos=new FileOutputStream(photo.getPath());
fos.write(jpeg);
fos.close();
}
catch(Exception e)
{
}
}

Categories

Resources