i'm using DrawingCache but it gives me NullPointerException
my code is as below:
myImageView.setDrawingCacheEnabled(true);
myImageView.buildDrawingCache();
resized = myImageView.getDrawingCache();
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String save_location = Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/EditedImage";
File dir = new File(save_location);
if (!dir.exists())
dir.mkdirs();
File f = new File(dir, TEMP_PHOTO_FILE);
FileOutputStream out;
try {
out = new FileOutputStream(f);
resized.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
error is at onClick.
my logcat is
what is missing in this?
Try this examle:
myImageView.setDrawingCacheEnabled(true);
myImageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
myImageView.layout(0, 0, v.getMeasuredWidth(), myImageView.getMeasuredHeight());
myImageView.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(myImageView.getDrawingCache());
myImageView.setDrawingCacheEnabled(false); // clear drawing cache
Updated:
Another way, you can create a Canvas for the Bitmap and then call view.draw(canvas) like:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
Each view will take some procedure, for example, measure and layout, before it is drew on screen. So when you invoke getDrawingCache() in Activity.onCreate(), the view is not drew yet.
Put the following two lines in your onclick method.
myImageView.buildDrawingCache();
resized = myImageView.getDrawingCache();
Related
I am using a scroll View in my activity.I want to share the whole activity screen but not able to do that.Am able to share the visible part of the screen but not the full screen of the activity.Is it possible to take the screenshot of the full activity.I have tried like this but its sharing only the visible part of the activity.Plz help me out of this.Thank You
ShareImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//View v1 = getWindow().getDecorView();
View v1 = findViewById(android.R.id.content);
v1.setDrawingCacheEnabled(true);
myBitmap = v1.getDrawingCache();
saveBitmap(myBitmap);
}
});
Saving the Screenshot taken ::
public void saveBitmap(Bitmap bitmap) {
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + "Pictures/screencapture.png";
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
share(filePath);
} catch (FileNotFoundException e) {
Log.e("exception1", e.getMessage(), e);
} catch (IOException e) {
Log.e("exception2", e.getMessage(), e);
}
}
try this:
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
I'm trying to capture all items in my ScrollView and save it as an image.
private void takeScreenShot()
{
ScrollView z = (ScrollView) findViewById(R.id.scroll_view);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
Bitmap b = getBitmapFromView(u,totalHeight,totalWidth);
//Save bitmap
String extr = Environment.getExternalStorageDirectory()+"/Folder/";
String fileName = "report.jpg";
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {
Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight , Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
The resulting image is enter image description here
There should be some text in the black area but it isn't showing.
If someone has a solution that may work, I would greatly appreciate it.
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
Just pass ScrollView or it's immediate child view to above function.
Try this, and let me know. Hope it will help !
Sometimes it'll set background to black if it isn't set to anything and it'll show up as black text on black background when you create the bitmap.
You can solve this by adding a background color in the .XML file. Be sure to add a background color to the view you want to capture and each item within the views as well. Anything that doesn't have a set background will show up with a black background.
When I get the bitmap from the DrawingCache of a view I get no error but I don't see a valid bitmap either. What am I doing wrong?
The code I use to generate the bitmap:
SharePhotoView sharePhotoView = SharePhotoView_.build(this);
sharePhotoView.bind(mCatch);
sharePhotoView.setDrawingCacheEnabled(true);
sharePhotoView.buildDrawingCache();
Bitmap bitmap = sharePhotoView.getDrawingCache();
catchImage.setImageBitmap(bitmap);
The code I use to make the view:
#EViewGroup(R.layout.share_photo)
public class SharePhotoView extends LinearLayout{
#ViewById
ImageView catchImage;
public SharePhotoView(Context context) {
super(context);
}
public void bind(Catch catchItem) {
Bitmap bitmap = BitmapFactory.decodeFile(catchItem.getImage().getPath());
catchImage.setImageBitmap(bitmap);
}
}
Use this code for a Bitmap from view:
Bitmap bitmap;
try {
bitmap = Bitmap.createBitmap(YOUR_VIEW.getWidth(), YOUR_VIEW.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
YOUR_VIEW.draw(canvas);
File root = Environment.getExternalStoragePublicDirectory(Environment.PICTURES);
String fname = "NAME_OF_FILE.jpg";
file = new File(root, fname);
try {
if (!root.exists()) {
root.mkdir();
}
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
YOUR_VIEW.destroyDrawingCache();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
}
Found a method somewhere that did the trick:
public static Bitmap getScreenViewBitmap(View v) {
v.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache
return b;
}
Source: http://www.jattcode.com/getting-bitmap-from-a-view-visible-invisible-oncreate/
I want to generate bitmap for webview full page.
some codes
Below code for generating Bitmap from webview
Webview to Bitmap :
webview.measure(MeasureSpec.makeMeasureSpec(
MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
webview.layout(0, 0, webview.getMeasuredWidth(),
webview.getMeasuredHeight());
webview.setDrawingCacheEnabled(true);
webview.buildDrawingCache();
Bitmap bm=null;
try
{
Log.d("Measuredwidth", "MeasuredWidth"+webview.getMeasuredWidth());
Log.d("Measuredheight", "Measuredheight"+webview.getMeasuredHeight());
Log.d("Measuredheightandstate", "Measuredheight and state"+webview.getMeasuredHeightAndState());
bm = Bitmap.createBitmap(webview.getMeasuredWidth(),
webview.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
}
catch(OutOfMemoryError e)
{
e.printStackTrace();
return null;
}
Canvas bigcanvas = new Canvas(bm);
Paint paint = new Paint();
int iHeight = bm.getHeight();
bigcanvas.drawBitmap(bm, 0, iHeight, paint);
webview.draw(bigcanvas);
below code for to save the file in memory for that
To save as file :
if (bm != null) {
try {
String path = Environment.getExternalStorageDirectory()
.toString();
OutputStream fOut = null;
File file = new File(path, "/aaaa.png");
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 50, fOut);
fOut.flush();
fOut.close();
bm.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
Here my problem is webview loading completely but after saving as file bottom half not completely loading .
i tried to get solution for this but failed.
if any one have idea about this please help me.. Thanks in adavance
My application has a list view in relative layout. Now I need to take a screen shot of the entire layout including all the content available in the list view and also other contents like button and text view showing in the layout. But my code takes only the visible part of the screen. Here the view is parent layout.
public void screen(){
View v1 = findViewById(R.id.screenRoot);
rel.layout(0, 0, rel.getMeasuredWidth(),rel.getMeasuredHeight());
View v = v1.getRootView();
v.setDrawingCacheEnabled(true);
/* v.measure(MeasureSpec.makeMeasureSpec(w, w),
MeasureSpec.makeMeasureSpec(h, h));*/
// v.layout(0, 0, rel.getWidth(), rel.getHeight());
v.buildDrawingCache(true);
System.out.println("rel "+rel.getHeight()+" "+v.getHeight());
Bitmap b = v.getDrawingCache();
v.setDrawingCacheEnabled(false);
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, "tiket"+".jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
System.out.println("my path "+myPath+" "+fos);
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Image Captured Successfully", 0).show();
}
Call getBitmapFromView from your Click event or any thing.
R.id.root is my main RelativeLayout So you can pass you main layout.
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout root = (RelativeLayout)inflater.inflate(R.layout.youlayourfile,null);
root.setDrawingCacheEnabled(true);
Bitmap bmp = getBitmapFromView(this.getWindow().getDecorView().findViewById(R.id.root).getRootView());
URI uri = storPrintFile(bmp);
This function returns the Bitmap of your layout. Now you just need to store the bitmap into your device and anywhere else.
public Bitmap getBitmapFromView(View v) {
v.setLayoutParams(new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
Storing bitmap into device.
public URI storPrintFile(Bitmap bitmapToStore){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
String path = cw.getDir("CapturedImages",Context.MODE_PRIVATE).getPath();
File file = new File(path);
boolean isFileCreated = false;
if (!file.exists()) {
isFileCreated = file.mkdir();
System.out.println("Directory created in Internal Storage is :" + path);
}
String current = "Screen.jpg";//uniqueId.replace(" ", "-").replace(":", "-") + ".jpeg";
System.out.println("Internal Storage path is : " + current);
File mypath = new File(file, current);
try {
FileOutputStream out = new FileOutputStream(mypath);
bitmapToStore.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return mypath.toURI();
}