2014-10-18 22 views

回答

2

您可以使用這樣一個方法:

@SuppressLint("SimpleDateFormat") 
protected final void shoot(final Context ctx, final View v, final String prefix) 
{ 

    // Get the bitmap from the view 
    v.setDrawingCacheEnabled(true); 
    final Bitmap bmp = v.getDrawingCache(); 

    final SimpleDateFormat sdf = new SimpleDateFormat("[email protected]"); 
    final Calendar cal = Calendar.getInstance(); 

    // Set file properties 
    fileJPG = prefix + "_" + sdf.format(cal.getTime()); 

    /* 
    Create a path where we will place our picture in the user's public 
    pictures directory. Note that you should be careful about what you 
    place here, since the user often manages these files. 
    For pictures and other media owned by the application, consider 
    Context.getExternalMediaDir(). 
    */ 
    final File path = 
     Environment.getExternalStoragePublicDirectory 
     (
      //Environment.DIRECTORY_PICTURES 
      //Environment.DIRECTORY_DCIM 
      Environment.DIRECTORY_DCIM + "/SomePath/" 
     ); 

    // Make sure the Pictures directory exists. 
    if(!path.exists()) 
    { 
     path.mkdirs(); 
    } 

    final File file = new File(path, fileJPG + ".jpg"); 

    try 
    { 
     final FileOutputStream fos = new FileOutputStream(file); 
     final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192); 

     bmp.compress(CompressFormat.JPEG, 85, bos); 

     bos.flush(); 
     bos.close(); 

     fileJPG = file.getPath(); 
    } 
    catch (final IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 

注意,你可以把你的APP的截圖只,而不是背後是什麼,即使它是半透明的。

+0

感謝它在繪製圖之前工作,但在繪製圖之後它給出了空指針異常並崩潰請幫助..... – Babith 2014-10-20 11:30:46

+0

我正在使用它與aChartEngine。它在繪製之後保存圖形:我首先在創建片段時繪製圖形,然後通過長時間點擊我截取屏幕截圖。 – 2014-10-20 11:52:23