2

これが私のScreenshot classです。アプリはスクリーンショットをギャラリーに保存しています。これは私が望んでいるものですが、画像は完全に黒です! この作業を行うための提案があれば、大歓迎です! ありがとう!

public class Screenshot extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

          // set event listener for the Save Contact Button
          Button button = 
             (Button) findViewById(R.id.button);
          button.setOnClickListener(buttonClicked);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.screenshot, menu);
        return true;
    }

       // responds to event generated when user clicks the Done Button
       OnClickListener buttonClicked = new OnClickListener() 
       {
          @Override
          public void onClick(View v) 
          {
              Bitmap bitmap = takeScreenshot();
              saveBitmap(bitmap);
           }         
       };


     public void saveBitmap(Bitmap bitmap) {
         LinearLayout mainLayout = (LinearLayout) findViewById(R.id.LinearLayout01);
         Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(), mainLayout.getHeight(), 
                 Bitmap.Config.ARGB_8888);
         MediaStore.Images.Media.insertImage(getContentResolver(), b, "image.png" , "screenshot");
    }

    public Bitmap takeScreenshot() {
         View rootView = findViewById(android.R.id.content).getRootView();
         rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();
        }
}

ここに私のmain.xmlがあります:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/LinearLayout01"
>
<Button
android:id="@+id/button"
android:layout_height = "wrap_content"
android:layout_width ="wrap_content"
android:text = "take screenshot"
android:onClick = "DoIt"
/>

<ImageView
android:id="@+id/eiffelTowerImageView"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_toRightOf="@+id/colosseumImageView"
android:src="@drawable/eiffeltower" />
</LinearLayout>
4

3 に答える 3

3

このコードは私にとってチャームのように機能します

private static Bitmap takeScreenShot(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b1 = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    int height = activity.getWindowManager().getDefaultDisplay()
            .getHeight();

    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
            - statusBarHeight);
    view.destroyDrawingCache();
    Log.e("Screenshot", "taken successfully");
    return b;

}

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory()
            + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        Log.e("Screenshot", "saved successfully");

        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }

}

OnCreate() でアクティビティを取得

Activity activity = (MainActivity) this;

次に、必要な場所でこれらの関数を呼び出します

Bitmap bitmap = takeScreenShot(activity);
saveBitmap(bitmap);
于 2015-07-24T07:07:26.720 に答える
0

このコードを試してください:

public class MainActivity extends Activity {

    Button btn_shoot;
    int i = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        btn_shoot = (Button)findViewById(R.id.btn_shoot);
        btn_shoot.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                    View view = findViewById(R.id.relativelayout);
                    view = view.getRootView();
                    view.setDrawingCacheEnabled(true);

                      view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

                          view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

                    Bitmap bitmap = view.getDrawingCache();

                    BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
                    ImageView iv = (ImageView) findViewById(R.id.imageView1);
                    iv.setBackgroundDrawable(bitmapDrawable);

                    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
                        //we check if external storage is available, otherwise display an error message to the user

                           File sdCard = Environment.getExternalStorageDirectory();
                           File directory = new File (sdCard.getAbsolutePath() + "/Tutorial_ScreenShot");
                           directory.mkdirs();

                           String filename = "screenshot" + i + ".jpg"; 
                           File yourFile = new File(directory, filename);

                           while (yourFile.exists()) {
                            i++;   
                            filename = "screenshot" + i + ".jpg"; 
                            yourFile = new File(directory, filename);
                           } 

                           if (!yourFile.exists()) {
                               if (directory.canWrite())
                               {
                                   try {
                                     FileOutputStream out = new FileOutputStream(yourFile, true);
                                     bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                                     out.flush();
                                     out.close();
                                     Toast.makeText(MainActivity.this, "File exported to /sdcard/Tutorial_ScreenShot/screenshot" + i + ".jpg", Toast.LENGTH_SHORT).show();
                                     i++;
                                    } catch (IOException e) {
                                   e.printStackTrace();
                                }

                               }
                           }

                        }
                        else
                        {
                           Toast.makeText(MainActivity.this, "SD Card not available!", Toast.LENGTH_SHORT).show();
                        }


            }
        });

    }

}
于 2014-04-07T11:46:54.843 に答える