1

すべての Android デバイスでスクリーン ショットを撮影できるわけではないことは承知していますが、ユーザーのゲーム画面のスクリーン ショットをプログラムで撮影して、友人などと共有できるようにしたいと考えています。

誰もこれを行う方法を知っていますか? ここに投稿されているいくつかのソリューションを試しました:

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   

// create bitmap screen capture
Bitmap bitmap;
View v1 = activity.getCurrentFocus().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

しかし、運がありません。これを行う他の方法はありますか?新しい方法かもしれません。

編集:

行 View v1 = activity.getCurrentFocus().getRootView(); で null ポインターを取得しています。

4

2 に答える 2

0

これを試してください これで、現在のレイアウトのスクリーンショットを撮り、SDカードに保存します...

スクリーンショットをキャプチャするには

于 2013-12-23T06:50:15.987 に答える
0

使用する

ビュー v1 = getWindow().getDecorView().getRootView();

代わりに

ビュー v1 = activity.getCurrentFocus().getRootView();

Null Pointer Exception はスローされません。

楽しみ!!

于 2014-09-29T09:53:34.490 に答える