2017-03-06 6 views
0

アクティビティサブレイアウトの一部を撮影したいと思います。 ..だから 活動自体が でframeLayoutされ、でframeLayoutはアンドロイドスタジオで1つの線画の写真を撮る方法は?

...ツールバー、ImageViewの、TextViewのなどのようないくつかの他のビューが含まれており、私はちょうどsublayoutを格納するために、私のSDカードにJPEG画像を作成したいと思います親のレイアウトの

私はGoogleで道を探すためにしようとしたと私はアンドロイドのスタジオで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"/> 
関連する問題