Bitmap.createBitmap返回null

时间:2014-09-22 10:42:17

标签: android bitmap screenshot

如果我在按钮clicklistener中调用下面的方法,我的代码可以工作。但是当我尝试直接从onCreate或button.performClick()

调用此代码时
bitmap = Bitmap.createBitmap(v1.getDrawingCache());

此行返回空指针异常。

public void takeScreenShot(){
    try{

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        Date date = new Date();
        String d = dateFormat.format(date);
        String mPath = Environment.getExternalStorageDirectory().toString() + "/ScreenShot/" + d + ".jpg";   
        String directory = Environment.getExternalStorageDirectory().toString() + "/ScreenShot/";
        File imageFile2 = new File(directory);
        if(!imageFile2.isDirectory()){
            imageFile2.mkdirs();
        }
        File imageFile;
        // create bitmap screen capture
        Bitmap bitmap;
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        OutputStream fout = null;
        imageFile = new File(mPath);


        try {
            fout = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
            fout.flush();
            fout.close();

        } catch (FileNotFoundException e) {
            Log.e("MAYDAY","hata11 " + e.toString());
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("MAYDAY","hata22 " + e.toString());
            e.printStackTrace();
        }
        ContentValues values = new ContentValues();
        values.put(Images.Media.TITLE, d + ".jpg");
        values.put(Images.Media.DESCRIPTION, d + ".jpg");
        values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis ());
        values.put(Images.ImageColumns.BUCKET_ID, d + ".jpg");
        values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, d + ".jpg");
        values.put("_data", imageFile.getAbsolutePath());

        ContentResolver cr = getContentResolver();
        cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    }catch(Exception e){
        Log.e("MAYDAY","hata33 " + e.toString());
    }

}

My LogCat

2 个答案:

答案 0 :(得分:1)

在初始化位图之前添加此代码

    v1.setDrawingCacheEnabled(true);

    // this is the important code :)  
    // Without it the view will have a dimension of 0,0 and the bitmap will 
    // be null          

    v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    v1.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

    v1.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
    v1.setDrawingCacheEnabled(false); 

这将设置位图的尺寸,否则它将为null。

答案 1 :(得分:0)

关于获取活动的快照(而不仅仅是单个视图),这是我几个月前的answers之一仍然适用:

  

实际上我前几天就这样做了,获得一个是痛苦的   整个Activity的截图,同时也确保删除   来自图像的StatusBar(因为它将显示为黑色矩形   在绘图缓存中)。此方法还允许您覆盖   返回的图像有一些颜色(即褪色)):

public static Bitmap getActivitySnapshot(Activity activity, boolean fade){
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    Bitmap bmap = view.getDrawingCache();

    Rect statusBar = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
    Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);

    if(fade && snapshot != null){
        Canvas canvas = new Canvas(snapshot);
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#88121212"));
        canvas.drawRect(0, 0, snapshot.getWidth(), snapshot.getHeight(), paint);
    }

    view.setDrawingCacheEnabled(false);
    return snapshot;
}   
  

如果你不想要褪色部分,那么重要的部分就是:

public static Bitmap getActivitySnapshot(Activity activity){
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    Bitmap bmap = view.getDrawingCache();

    Rect statusBar = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
    Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);

    view.setDrawingCacheEnabled(false);
    return snapshot;
}
  

您可能还想捕获可能抛出的OutOfMemoryError   使用大型位图时。