Android 为特定视图拍摄快照面临的问题

Android 为特定视图拍摄快照面临的问题,android,Android,当我在android中拍摄特定视图的快照时。截图后,我的整个视图将是空白的。请检查我的全部代码,我哪里做错了。我在谷歌上搜索了这么多的引语,但没能解决我的问题。请有人帮帮我 // here is my code fb_share_btn.setOnClickListener (new View.OnClickListener ( ) { @Override public void onClick(View view) {

当我在android中拍摄特定视图的快照时。截图后,我的整个视图将是空白的。请检查我的全部代码,我哪里做错了。我在谷歌上搜索了这么多的引语,但没能解决我的问题。请有人帮帮我

   // here is my code

 fb_share_btn.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View view) {
                boolean checkPermission = checkPermission();
                /*Bitmap bitmap = takeScreenshot();*/

                Bitmap bitmap = loadBitMapFromView(findViewById (R.id.tv_screenshot),findViewById (R.id.tv_screenshot).getWidth (),findViewById (R.id.tv_screenshot).getHeight ());

                saveBitmap(bitmap);
                shareIt();
            }
        });

// save bitmap function

public void saveBitmap(Bitmap bitmap) {
        imagePath = new File (Environment.getExternalStorageDirectory ()+ "/screenshot.png");


        Log.i ("Message","Testingabc:"+ imagePath);
        FileOutputStream fos;
        try {
            fos = new FileOutputStream (imagePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e("GREC", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }
    }

    private Bitmap loadBitMapFromView(View v, int width, int height) {
        Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas (b);
        c.drawColor (Color.WHITE);


        v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
        v.draw(c);
        return b;
    }
    private void shareIt() {
        Uri uri = FileProvider.getUriForFile(TimeCounter.this, BuildConfig.APPLICATION_ID + ".provider",imagePath);
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("image/*");
        String shareBody = "In Tweecher, My highest score with screen shot";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Tweecher score");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
        sharingIntent.setPackage("com.facebook.katana");

        startActivity(sharingIntent);
    }
}

将此代码与AsycTask一起使用

 @Override
        protected void onPreExecute() {
 try {
                View v1 = getWindow().getDecorView().getRootView();
                v1.setDrawingCacheEnabled(true);
                bitmap = Bitmap.createBitmap(v1.getDrawingCache());
                v1.setDrawingCacheEnabled(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
}

@Override
        protected Integer doInBackground(Integer... integers) {
            try {
                File root = new File(Environment.getExternalStorageDirectory(), "/Screenshot/");
                if (!root.exists()) {
                    root.mkdirs();
                }
                imageFile = new File(root.toString() + "/" + imageName + ".jpg");
                FileOutputStream outputStream = new FileOutputStream(imageFile);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 75, outputStream);
                outputStream.flush();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return 0;
        }
}

你在模拟器上测试吗?