android - 截图显示黑色

标签 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);

谁能告诉我这段代码有什么问题。

编辑

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"));

        }

    };

黑图 enter image description here

最佳答案

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

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();
}

关于android - 截图显示黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16610404/

相关文章:

java - 为什么谷歌地图 v2 突然被 java.lang.NullPointerException : Attempt to get length of null array? 崩溃

android - 如何使用 Android API 录屏和截屏?

html - 将网页转换为图像

android - 溢出-x : hidden; not working on android devices

android - 如何在 Android O 上使用 StorageStatsManager.queryStatsForPackage?

testing - 有没有办法让 'select' 选项打开截图?

iOS 获取 UITableView 可见部分的屏幕截图

javascript - phantomjs 不等待页面完全加载

android - 谷歌 Agera 与 rxAndroid

android - 如何制作多搜索对话框?