2013-03-07 38 views
5

私は、現在の画面のスクリーンショット(ボタンを押した後)をキャプチャしてギャラリーに保存するコードを知っていますが、これはSDカードを備えたデバイスがないためです。だから私はデフォルトのギャラリーに保存する。ありがとうございましたプログラムでスクリーンショットを撮ってギャラリーに保存する方法は?

+0

端末がルートされていないとできません。 – 323go

+0

[http://stackoverflow.com/questions/7762643/android-take-screen-shot-programatically][1] .....これを試してみてください[1]:のhttp:/ /stackoverflow.com/questions/7762643/android-take-screen-shot-programatically –

答えて

0

323goとコメントしました。あなたのデバイスが実際にルートされていない限り、これはコメントできません。

もしそうなら、monkeyrunnerの場合や、エミュレータを使用している場合はうまくいくかもしれません。

9
Bitmap bitmap; 
    View v1 = findViewById(R.id.rlid);// get ur root view id 
    v1.setDrawingCacheEnabled(true); 
    bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
    v1.setDrawingCacheEnabled(false); 

これはトリックを行う必要があります。完全なソースコードについては

ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 
    File f = new File(Environment.getExternalStorageDirectory() 
        + File.separator + "test.jpg") 
    f.createNewFile(); 
    FileOutputStream fo = new FileOutputStream(f); 
    fo.write(bytes.toByteArray()); 
    fo.close(); 
+0

私は上記の答えを試してみましたか? – Raghunandan

+2

うまく動作し、良い答えです。パーミッションを追加することを忘れないでください、または動作しません:

4
View v1 = L1.getRootView(); 
    v1.setDrawingCacheEnabled(true); 
    Bitmap bm = v1.getDrawingCache(); 
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bm); 
    image = (ImageView) findViewById(R.id.screenshots); 
    image.setBackgroundDrawable(bitmapDrawable); 

を保存するための

Android Saving created bitmap to directory on sd card

以下のリンクを参照してビットマップを格納するための以下のブログ

を通じて

http://amitandroid.blogspot.in/2013/02/android-taking-screen-shots-through-code.html

を行きます

1

これはギャラリーに保存されます。このコードは、画像のパスも設定します。これは、Intent.SEND_ACTIONやEメールインテントで便利です。

String imagePath = null; 
Bitmap imageBitmap = screenShot(mAnyView); 
if (imageBitmap != null) { 
    imagePath = MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, "title", null); 
} 


public Bitmap screenShot(View view) { 
    if (view != null) { 
     Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), 
       view.getHeight(), Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 
     view.draw(canvas); 
     return bitmap; 
    } 
    return null; 
} 
+0

これは数ヶ月ですが、それはビューの内容をキャプチャしましたが、サブビューはキャプチャしませんでした。ビューの上に表示されるすべてのもの(別名、画面全体)のビットマップをどのように取得しますか? – ByteSlinger

関連する問題