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.
Related
Im trying to save a profile photo, but when I use the save image code I got (i got almost the same example everywhere on the internet), I don't really know where the image is getting stored.
public void saveImage(Bitmap image) {
FileOutputStream out = null;
try {
out = new FileOutputStream("BecityAvatar.png");
image.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
The thing is, I want to store the image so when the profile launches and the layout is loaded, if there is an image stored as the avatar, it loads itself onto the imageview from the layout. If there isn't, nothing will happen. I don't really know where the images will get stored when I save them. Any help or tips will be appreciated.
Your problem is with:
out = new FileOutputStream("BecityAvatar.png");
this way you are creating a FileOutputStream, that points to /, and your application has not right to write there. For instance
File file = new File(getFilesDir(), "BecityAvatar.png")
out = new FileOutputStream(file);
to use getFilesDir(), you need a context
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.
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!!
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 :)
I am using the following code to take a screenshot of a TableLayout. I must add that the theme of my android application is set to LIGHT. It shows perfectly fine on the emulator screen (just like it is supposed to)....however, once the screenshot is taken, the image turns out to be like this...can anyone help point out what I am doing wrong here? Thanks!
private void getScreen()
{
View content = findViewById(R.id.TransactionLog);
content.setDrawingCacheEnabled(true);
content.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());
content.setDrawingCacheEnabled(false); // clear drawing cache
File file = new File(Environment.getExternalStorageDirectory() +
File.separator + "logDetails.jpeg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.flush();
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
if (!file.exists()) {
sendmail();
}
}
1) you should use png. Jpeg is for photos (basically).
2) because you're using Jpeg, I assume those black areas would otherwise be transparent. Png supports transparency. Jpeg does not. I have not tried drawingCache, so maybe it doesn't support transparency either and I'm totally wrong, but that's my guess.
So, summary, try Png.