Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从android中的活动获取屏幕截图视图?_Android_Screenshot - Fatal编程技术网

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

从android中的活动获取屏幕截图视图?,android,screenshot,Android,Screenshot,我想尝试为android实现一个屏幕截图功能。为此,我想从android的活动中查看。我不允许使用R.id.viewid,因为我将其作为库的一部分而不是应用程序来实现。所以我尝试使用activity.getWindow.getDecorView 请让我知道得到DecorView是否是正确的方法?或者应该有另一种方法 MyActivity.java @Override protected void onResume(){ super.onResume();

我想尝试为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)

尝试为此替换getWindow.getDecorView

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

我认为其余的方法也可以

尝试将此方法置于onPause中,因为视图准备在onResume期间进行任何修改。因此,要么在onResume中放置一些延迟,要么使用处理程序以异步方式拍摄屏幕截图,或者如果屏幕截图不是那么关键,则在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;
        }
    }

好啊所以装饰视图为空。我不知道这一点,但是的,应用程序在位图返回位图=Bitmap.createBitmapview.getWidth,view.getHeight,Bitmap.Config.ARGB_8888时崩溃;getWidth调用为null的行,因此装饰视图可能为null。@downvote:我可以知道downvote的原因吗?不,这对我没有帮助,因为我将此创建为库的一部分。所以我不能要求用户提供视图。@NileshAgrawal-仅供参考-这不是用户提供的视图。正如您所看到的,它是一个来自android.R.ID名称空间的ID——它是包装用户内容的布局——因此总是存在的。