屏幕截图显示黑色

Dil*_*lip 8 android screenshot

我正在拍摄快照并制作缩略图,然后分享这张图片.但缩略图显示全黑.我使用了以下代码

Bitmap bitmap;
View v1 = v.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
String url = Images.Media.insertImage(
mContext.getContentResolver(), bitmap, "title", null);
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我这段代码有什么问题.

编辑

private View.OnClickListener shareListener = new View.OnClickListener() {
        public void onClick(View v) {
            Bitmap bitmap;
            View v1 = v.getRootView();
            v1.setDrawingCacheEnabled(true);
            bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            String url = Images.Media.insertImage(
                    mContext.getContentResolver(), bitmap, "title", null);
            v1.setDrawingCacheEnabled(false);
            Activity activity = (Activity) getContext();
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");
            share.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
            activity.startActivity(Intent.createChooser(share,
                    "Share"));

        }

    };
Run Code Online (Sandbox Code Playgroud)

黑色图像 在此输入图像描述

SAU*_*_12 3

使用以下代码可能对您有用。谢谢

SCREENSHOTS_LOCATIONS = Environment.getExternalStorageDirectory().toString() + "/screenshots/";
// Get root view
View view = activity.getWindow().getDecorView().getRootView();
// Create the bitmap to use to draw the screenshot
final Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);

// Get current theme to know which background to use
final Theme theme = activity.getTheme();
final TypedArray ta = theme
    .obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
final int res = ta.getResourceId(0, 0);
final Drawable background = activity.getResources().getDrawable(res);

// Draw background
background.draw(canvas);

// Draw views
view.draw(canvas);

// Save the screenshot to the file system
FileOutputStream fos = null;
try {
    final File sddir = new File(SCREENSHOTS_LOCATIONS);
    if (!sddir.exists()) {
        sddir.mkdirs();
    }
    fos = new FileOutputStream(SCREENSHOTS_LOCATIONS
            + System.currentTimeMillis() + ".jpg");
    if (fos != null) {
        if (!bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) {
            Log.d("ScreenShot", "Compress/Write failed");
        }
        fos.flush();
        fos.close();
    }

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)