2017-03-06 56 views
0

我想拍一部分活动子版面的图片。 所以.. 活动本身会的FrameLayout 和FrameLayout里包括像工具栏的ImageView,TextView的等一些其他的看法..如何拍摄android studio中的一个线性布局?

,我想作一个JPEG图片到我的SD卡,以便存储只是一个sublayout父母的布局。

我试图寻找对谷歌的方式,我发现已经在Android Studio中被obsolated解决

预先感谢您

回答

0

完成完整的源如..

private void snapScreen() { 
    View v1 = getWindow().getDecorView().getRootView(); 
    // View v1 = iv.getRootView(); //even this works 
    View v1 = findViewById(R.id.main_result); //this works too for particular view 
    // but gives only content 
    Bitmap myBitmap; 
    v1.setDrawingCacheEnabled(true); 
    myBitmap = v1.getDrawingCache(); 
    snap.saveBitmap(myBitmap, IntentResultItem); 
} 

对于创建快照

确保您同意外部存储

public class SendSnap { 

Context mContext; 

public SendSnap(Context context) { 
    this.mContext = context; 
} 


public void saveBitmap(Bitmap bitmap, String msg) { 

    String filePath = Environment.getExternalStorageDirectory() 
      + File.separator + "screenshot.png"; 
    File imagePath = new File(filePath); 
    FileOutputStream fos; 
    try { 
     fos = new FileOutputStream(imagePath); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     fos.flush(); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     Log.e("GREC", e.getMessage(), e); 
    } catch (IOException e) { 
     Log.e("GREC", e.getMessage(), e); 
    } 
} 

}

2
//Define a bitmap with height and width of the View 
Bitmap viewBitmap = Bitmap.createBitmap(v.getWidth(),v.getHeight(),Bitmap.Config.RGB_565); 

Canvas viewCanvas = new Canvas(viewBitmap); 

//get background of canvas 
Drawable backgroundDrawable = v.getBackground(); 

if(backgroundDrawable!=null){ 
backgroundDrawable.draw(viewCanvas);//draw the background on canvas; 
} 
else{ 
viewCanvas.drawColor(Color.GREEN); 
//draw on canvas 
v.draw(viewCanvas) 
} 

//write the above generated bitmap to a file 
String fileStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     OutputStream outputStream = null; 
     try{ 
      imgFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),fileStamp+".jpg"); 
      outputStream = new FileOutputStream(imgFile); 
      b.compress(Bitmap.CompressFormat.JPEG,40,outputStream); 
      outputStream.close(); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 

不要忘记添加此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>