android app screenshot明确的布局

时间:2016-04-13 10:05:36

标签: android layout screenshot

android app截图明确布局,我有问题,截图后,图像是布局上的黑色异常图像

  

这是我的代码

{   

Bitmap screenshot =getscreenshot((LinearLayout)findViewById(R.id.question_main));

String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), screenshot, "title", null);

Uri bitmapUri = Uri.parse(bitmapPath);

Intent sendIntent = new Intent(Intent.ACTION_SEND);

sendIntent.setType("image/*");

sendIntent.putExtra(Intent.EXTRA_STREAM, bitmapUri);

startActivity(Intent.createChooser(sendIntent, "Share screenshot"));

}       


private Bitmap getscreenshot(View view) {

    View v = view;

    v.setDrawingCacheEnabled(true);

    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());

    return bitmap;

}

真实截图:

enter image description here

我的代码截图:

enter image description here

2 个答案:

答案 0 :(得分:0)

试试这段代码:

 private Bitmap getscreenshot() {

    SimpleDateFormat date= new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss").format(new Date());
        try {

            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + date+ ".jpg";

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

            File imageFile = new File(mPath);

            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();

          return bitmap;
        } catch (Throwable e) {

            e.printStackTrace();
        }
    return null;
}

答案 1 :(得分:0)

  

我想只截取“screenshotlayout”布局。

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mylayout">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/sual_imagebox"/>
       <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/screenshotlayout">
            <io.github.kexanie.library.MathView
                android:id="@+id/sual"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                auto:text="text"
                auto:engine="MathJax"
                android:layout_below="@+id/bookmark">
            </io.github.kexanie.library.MathView>
        </LinearLayout>
  </RelativeLayout>