I am working on android. I have following code to capture a screenshot.
Setting up your Root layout:
View content = findViewById(R.id.layoutroot);
content.setDrawingCacheEnabled(true);
Function to get the rendered view:
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();
}
}
My problem is that it creates a zero kb file. what could be problem?
do these changes and try again:
make the content variable a final and call findViewById on the layout of the activity
final View content = somelayout.findViewById(R.id.layoutroot);
You have not write data to the file. you are directly closing the file thats why you got a file with 0 kb
use something like this
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
byte[] mydata = null;//data in byte array
ostream.write(mydata);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Related
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.
I am trying to create bitmap of all view.but I am not able to create bitmap of all view,please help me to resolve this problem.
public Bitmap overlay(FrameLayout frame_layout){
frame_layout.setDrawingCacheEnabled(true);
frame_layout.buildDrawingCache();
return frame_layout.getDrawingCache();
}
public void saveImage(){
Bitmap bitmap=overlay(print_layout);
try{
String path = Environment.getExternalStorageDirectory().toString()+ File.separator+"bitmap";
File path_file=new File(path);
path_file.mkdirs();
OutputStream fOut = null;
File file = new File(path+ File.separator+"print.png"); // the File to save to
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush();
fOut.close();
Log.d("sun","saved");
}
catch (Exception e){
Log.d("sun",e.toString());
}
}
Im using the code as shown bellow for saving a jpg file to sdcard on a button click.
OutputStream fOut = null;
try
{
fOut = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
mMergedLayersBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
for saving the image to a folder, I rewrite the code as shown bellow
OutputStream fOut = null;
//Create Folder
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/draw/Images");
folder.mkdirs();
//Save the path as a string value
String extStorageDirectory = folder.toString();
//Create New file and name it draw.jpg
File file = new File(extStorageDirectory, "draw.jpg");
try
{
fOut = new FileOutputStream(file);
mMergedLayersBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
Now its rewriting the image when I am click the save button second time. I want to save the whole image that im saving to a folder. I don't know how to change my code for this requirement. If anyone know about this, please help me..
Since you have hardcoded the name of the image as "draw.jpg", each time your image gets overwritten. So instead of hardcoding provide a dynamic name.
For Example, instead of,
File file = new File(extStorageDirectory, "draw.jpg");
try
File file = new File(extStorageDirectory, System.currentTimeMillis()+"draw.jpg");
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 facing some strange problem with my android code,
I have an image in Bitmap variable and want to save that file to SD card.
I code as follow,
Bitmap IMAGE // Loaded from internet servers.;
try {
File _sdCard = Environment.getExternalStorageDirectory();
File _picDir = new File(_sdCard, "MyDirectory");
_picDir.mkdirs();
File _picFile = new File(_picDir, "MyImage.jpg");
FileOutputStream _fos = new FileOutputStream(_picFile);
IMAGE.compress(Bitmap.CompressFormat.JPEG, 100, _fos);
_fos.flush();
_fos.close();
Toast.makeText(this, "Image Downloaded", 7000).show();
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(this, ex.getMessage(), 7000).show();
}
I am using Sony Experia Arc as my testing device, when the phone is connected to my computer, the code works nice, it stores image and also displays in gallery. But when I disconnect phone from my computer and test the app, it doesn't save picture and doesn't show any exception.
use this function
void saveImage() {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
String fname = "Image.jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Check this answer will give more details Android saving file to external storage
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//4
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
//5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
**This Code Cover the Following Topics**
1. Save a bitmap Image on sdcard a jpeg
2. Create a folder on sdcard
3. Create every file Separate name
4. Every file save with date and time
5. Resize the image in very small size
6. Best thing image Quality fine not effected from Resizing
The following method is used to create an image file using the bitmap
public void createImageFromBitmap(Bitmap bmp) {
FileOutputStream fileOutputStream = null;
try {
// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Capture/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
//Capture is folder name and file name with date and time
fileOutputStream = new FileOutputStream(String.format(
"/sdcard/Capture/%d.jpg",
System.currentTimeMillis()));
// Here we Resize the Image ...
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100,
byteArrayOutputStream); // bm is the bitmap object
byte[] bsResized = byteArrayOutputStream.toByteArray();
fileOutputStream.write(bsResized);
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
and add this in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />