Android screenshot from code. Got it but not perfe

2019-03-14 04:42发布

问题:

I'm trying to take a screenshot in code in Android. Actually the screenshot is the bitmap of the main RelativeLayout. The screenshot is taken but the content appears wrong, the fill_parent, etc tags are not respected and the image appears in the top left and with the original size.

Please anyone, help.

Thanks

This is my code :

RelativeLayout v = (RelativeLayout)findViewById(R.id.main_layout);
        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(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        //v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
        v.layout(0, 0, v.getWidth(), v.getHeight());
        v.buildDrawingCache(true);
        Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());
        v.setDrawingCacheEnabled(false); //

        if (bm != null) {
            try {
                String path = Environment.getExternalStorageDirectory()
                        .toString();
                OutputStream fOut = null;
                File file = new File(path, "screentest.jpg");
                fOut = new FileOutputStream(file);

                bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                fOut.flush();
                fOut.close();

                Log.e("ImagePath", "Image Path : "
                        + MediaStore.Images.Media.insertImage(
                                getContentResolver(), file.getAbsolutePath(),
                                file.getName(), file.getName()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

And this is the layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/backImage" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 />

<com.calendarView.CalendarView
    android:orientation="vertical"
    android:id="@+id/calendar_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/> 

</RelativeLayout>

@ns476

回答1:

Specify the screen size in makeMeasureSpec

...
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
v.measure(MeasureSpec.makeMeasureSpec(0, size.x),
            MeasureSpec.makeMeasureSpec(0, size.y));
...

Also try setting the setDrawingCacheQuality to high, so that it returns a higher resolution bitmap.



回答2:

I think you need to replace the

RelativeLayout v = (RelativeLayout)findViewById(R.id.main_layout);

v.setDrawingCacheEnabled(true);

v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

v.layout(0, 0, v.getWidth(), v.getHeight());

v.buildDrawingCache(true);

Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());

v.setDrawingCacheEnabled(false);

with

Bitmap bitmap;

View root = findViewById(android.R.id.content);

View v1 = root.getRootView();

v1.setDrawingCacheEnabled(true);

root.setDrawingCacheEnabled(true);

Bitmap bm = Bitmap.createBitmap(root.getDrawingCache());

root.setDrawingCacheEnabled(false);


回答3:

I'm assuming you're doing this during creation. Don't!

use a postDelayed(Runnable, int) to execute the Bitmap bm = Bitmap.createBitmap(v.getDrawingCache()); after the view is drawn.

also, remove the v.measure() and v.layout() those are methods for the ViewGroup to be calling.



回答4:

Remove the lines:

v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.layout(0, 0, v.getWidth(), v.getHeight());


回答5:

LinearLayout ll = (LinearLayout )findViewById(R.id.layout); ll .setDrawingCacheEnabled(true); Bitmap bm = v1.getDrawingCache();

Try this



回答6:

This works good for me:

View content = findViewById(R.id.myElementForScreenshoot);
View screenshot = content; // or content.getRootView() for fullscreen
screenshot.refreshDrawableState();
screenshot.setDrawingCacheEnabled(true);
Bitmap b = screenshot.getDrawingCache();    

After this you can process or save bitmap. Hope it helped. Cheers