2011-06-22 9 views
-2
 Button button = (Button)findViewById(R.id.btnTakeScreenshot); 
     button.setOnClickListener(new View.OnClickListener() { 
     //@Override 
     public void onClick(View v) { 
     final View content = findViewById(R.id.layoutroot); 
     content.setDrawingCacheEnabled(true); 
     Bitmap bitmap = content.getDrawingCache(); 
     File file = new File(Environment.getExternalStorageDirectory() + "/test.png"); 
     try 
     { 
      file.createNewFile(); 
      FileOutputStream ostream = new FileOutputStream(file); 
      bitmap.compress(CompressFormat.PNG, 100, ostream); 
      ostream.close(); 
      Toast.makeText(content.getContext(), "HELLO", Toast.LENGTH_SHORT); 
     } 
     catch (Exception e) 
     { 
     System.out.print("error"); 
      e.printStackTrace(); 
     } 
} 
}); 

上記のコードはスクリーンショットをキャプチャすることですが、ゼロkbの空白ファイルを作成しますか?android screenshot?

+0

例外はありますか? – Egor

+0

いいえ...何も例外をスローしません...問題はイメージの保存/キャプチャにあります.. – alpesh

+0

あなた自身でその問題を解決しようとしたようには見えません。 –

答えて

2

file.createNewFile();あなたが尋ねたこととまったく同じです:空の空のファイルを作成します。描画キャッシュが空の場合は、ファイルに何も表示されません。試してみてください:

Button button = (Button)findViewById(R.id.btnTakeScreenshot); 
     final View content = findViewById(R.id.layoutroot); 
     content.setDrawingCacheEnabled(true); 
     button.setOnClickListener(new View.OnClickListener() { 
     //@Override 
     public void onClick(View v) { 
     Bitmap bitmap = content.getDrawingCache(); 
     File file = new File(Environment.getExternalStorageDirectory() + "/test.png"); 
     try 
     { 
      file.createNewFile(); 
      FileOutputStream ostream = new FileOutputStream(file); 
      bitmap.compress(CompressFormat.PNG, 100, ostream); 
      ostream.close(); 
      Toast.makeText(content.getContext(), "HELLO", Toast.LENGTH_SHORT); 
     } 
     catch (Exception e) 
     { 
     System.out.print("error"); 
      e.printStackTrace(); 
     } 
} 
}); 
+0

ちょっと男...それは働いて.....素晴らしい解決策femi ...ありがとうso muh – alpesh