从android中的活动获取视图以获取截图?

时间:2015-02-10 21:48:24

标签: android screenshot

我想尝试为android实现截图功能。为此我想要一个来自android的活动的视图。我不允许使用R.id.viewid,因为我将其作为库的一部分而不是应用程序实现。所以我试图使用activity.getWindow().getDecorView 如果获得DecorView是正确的方法,请告诉我?或者应该有另一种方法。

MyActivity.java

 @Override
    protected void onResume(){
        super.onResume();    
        String mPath = Environment.getExternalStorageDirectory().toString() + "/test";
        Screenshot.takeScreenshot (this.getWindow().getDecorView(), mPath);
        imageView.setImageBitmap(Screenshot.getBitmapScreenshot(this.getWindow().getDecorView()));
    }

Screenshot.java

public static void takeScreenshot(View view, String filePath) {
        Bitmap bitmap = getBitmapScreenshot(view);
        File imageFile = new File(filePath);
        imageFile.getParentFile().mkdirs();
        try {
            OutputStream fout = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
            fout.flush();
            fout.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

public static Bitmap getBitmapScreenshot(View view) {

        //Define a bitmap with the same size as the view
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
        //Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        //Get the view's background
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null)
            //has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else
            //does not have background drawable, then draw white background on the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        view.draw(canvas);
        //return the bitmap
        return returnedBitmap;


    }

我收到了以下错误。

    3795-3795/com.screenshot.nileshagrawal.screenshotapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.screenshot.nileshagrawal.screenshotapp, PID: 3795

        java.lang.RuntimeException: Unable to resume activity {com.screenshot.nileshagrawal.screenshotapp/com.screenshot.nileshagrawal.screenshotapp.MainActivity}: 

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getWidth()' on a null object reference
                at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2951)
                at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getWidth()' on a null object reference
                at com.screenshot.nileshagrawal.screenshotapp.Screenshot.getBitmapScreenshot(Screenshot.java:49)
                at com.screenshot.nileshagrawal.screenshotapp.Screenshot.takeScreenshot(Screenshot.java:24)
                at com.screenshot.nileshagrawal.screenshotapp.MainActivity.onResume(MainActivity.java:32)
                at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
                at android.app.Activity.performResume(Activity.java:6019)
                at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2940)
                at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

2 个答案:

答案 0 :(得分:1)

尝试将此方法放在onPause()中,因为视图已准备好在onResume()期间进行任何修改。所以要么在onResume中加一些延迟(使用处理程序以异步方式截取屏幕截图)或者如果屏幕截图不是critical在onPause()中采用相同的方法,并在super.onPause()调用之前调用该函数。在其中一个stackoverflow post中,下面的代码示例正在运行。  我在onPause()中测试了相同的内容。

使用此方法

 public static String takeScreenshot(Activity activity) {
        Bitmap bitmap = null;
        Bitmap resultBitmap = null;
        ByteArrayOutputStream bos = null;
        String encodedImg = null;
        Toast.makeText(activity.getBaseContext(), "Taking screenshot",
                Toast.LENGTH_SHORT).show();
        try {
            View v1 = activity.getWindow().getDecorView().getRootView();

            v1.setDrawingCacheEnabled(true);
            bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            resultBitmap = Bitmap.createScaledBitmap(bitmap,
                    v1.getMeasuredWidth(), v1.getMeasuredHeight(), false);
            v1.setDrawingCacheEnabled(false);

            //MediaStore.Images.Media.insertImage(activity.getContentResolver(),
            //resultBitmap, "myScreenshot2", "Test2");

            bos = new ByteArrayOutputStream();
            resultBitmap.compress(Bitmap.CompressFormat.JPEG, 10, bos);
            byte[] bitmapdata = bos.toByteArray();
            encodedImg = new String(Base64.encode(bitmapdata, Base64.DEFAULT));

        } catch (Exception e) {
            Log.e(Constants.LOG_TAG, "Could not capture the screen image");
            Log.e(Constants.LOG_TAG, e.toString());
            Log.e(Constants.LOG_TAG, Arrays.toString(e.getStackTrace()));
        } finally {
            if (bitmap != null) {
                bitmap.recycle();
            }
            if (resultBitmap != null) {
                resultBitmap.recycle();
            }
            if (bos != null) {
                bos.reset();
            }
            return encodedImg;
        }
    }

答案 1 :(得分:-2)

尝试用此替换getWindow()。getDecorView

getWindow().getDecorView().findViewById(android.R.id.content)

我认为其余的会有用