2017-02-18 6 views
0

私はprogramatically.it workで私の活動のスクリーンショットを作成しました。私のactivity.howに表示されているイメージを撮影した後に問題があります。それを隠すには?私がスクリーンショットを撮ると、スクリーンショットイメージが私のactivity.howに隠されて表示されますか?

Mycode:

public class MainActivity extends AppCompatActivity { 

    File cacheDir; 
    final Context context = this; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button print = (Button) findViewById(R.id.btn_print); 

     print.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       takeScreenShot(); 
      } 
     }); 



    } 

     private void takeScreenShot() { 

     View u = findViewById(R.id.activity_main); 

     int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
     u.measure(spec, spec); 
     u.layout(0, 0, u.getMeasuredWidth(), u.getMeasuredHeight()); 

     Bitmap b = getBitmapFromView(u,u.getMeasuredHeight(),u.getMeasuredWidth()); 

     final String root = Environment.getExternalStorageDirectory().toString(); 

     File myPath = new File(root + "/saved_img"); 
     myPath.mkdirs(); 
     Random generator = new Random(); 
     int n = 10000; 
     n = generator.nextInt(n); 
     String fname = "Image-"+n+".jpg"; 
     File file = new File(myPath, fname); 
     FileOutputStream fos = null; 
     if(file.exists()) file.delete(); 
     try{ 
      fos = new FileOutputStream(file); 
      b.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
      fos.flush(); 
      fos.close(); 


     } catch(FileNotFoundException e){ 
      e.printStackTrace(); 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 

     Toast.makeText(this,"screen captured",Toast.LENGTH_SHORT).show(); 
    } 



    public Bitmap getBitmapFromView(View u, int totalHeight, int totalWidth){ 
     Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight , Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(returnedBitmap); 

     Drawable bgDrawable = u.getBackground(); 
     if (bgDrawable != null) 
      bgDrawable.draw(canvas); 
     else 
      canvas.drawColor(Color.WHITE); 
     u.draw(canvas); 
     return returnedBitmap; 
    } 
} 

これを解決する方法?前にクリックボタンのための私の出力画像、

enter image description here

は、このボタンをクリックした後のための私の出力2、 enter image description here

+0

?スクリーンショットのみが必要ですか?あなたの活動の権利にはそれが示されていませんか? –

+0

yes.correct.howアクティビティのスクリーンショットイメージを非表示にするには... –

+0

getBitmapFromviewを使用しないで、アクティビティ全体のアクティビティの高さと幅を取得する方法は? –

答えて

0

それだ私は画像サイズを取得する上でレイアウトサイズを設定し、私のmistake.afterきスクリーンショット(あります私はプログラムの下の行を完全に動作する削除してください。&の高さを取得します。

u.layout(0、0、u.getMeasuredWidth()、u.getMeasuredHeight());

ためerror.iは再びall.thanksのためにあなたの助けのためのit.thanksを解くの現在のlayout.soにそのラインセット保存した画像の正確な高さ&幅... uは何をする必要があるか

関連する問題