在android上捕获当前活动屏幕

在android上捕获当前活动屏幕,android,android-canvas,screenshot,android-maps-v2,android-bitmap,Android,Android Canvas,Screenshot,Android Maps V2,Android Bitmap,我试图捕捉当前活动的屏幕,以便使用android的共享表共享图像。“我的活动”包含地图片段和文本视图。我尝试过的每一件事都捕捉到了一个屏幕截图 (在画廊中显示为黑色)。我应该如何捕捉整个屏幕?以下是我使用的XML文件和java代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height

我试图捕捉当前活动的屏幕,以便使用android的共享表共享图像。“我的活动”包含地图片段和文本视图。我尝试过的每一件事都捕捉到了一个屏幕截图 (在画廊中显示为黑色)。我应该如何捕捉整个屏幕?以下是我使用的XML文件和java代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="15"
android:gravity="bottom"
android:id="@+id/rootview"
android:background="#e7ebed">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/under_root"
    android:layout_weight="1">

    <fragment
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map2"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="330dp"
    tools:context="com.github.pires.obd.reader.activity.TripReport" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="227dp"
        android:layout_weight="1">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.66">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Average speed"
                android:id="@+id/avgspeed"
                android:layout_weight="1"
                android:textColor="#000000" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Engine Runtime"
                android:id="@+id/runtime"
                android:layout_weight="1"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.66">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Distance"
                android:id="@+id/dist"
                android:layout_weight="1"
                android:textColor="#000000" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Idle Time"
                android:id="@+id/idletime"
                android:layout_weight="1"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.66">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/totaltime"
                android:layout_weight="1" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView2"
                android:layout_weight="1" />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="bottom">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share"
        android:id="@+id/sharebutton"
        android:layout_weight="1"
        android:background="#7986cb"
        android:textColor="#ffffff"
        android:onClick="shareAction" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Quit"
        android:id="@+id/butt_quit"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:background="#e53935"
        android:onClick="exitApp" />
</LinearLayout>
</LinearLayout>

Maps V2 API有自己的屏幕截图选项。我的布局包括地图和文本视图,API是否同时捕获了两者?我对此表示怀疑,尽管我没有使用它的API。我猜你必须以某种方式从不同的片段中组装一个完整的屏幕截图。
       public void takeScreenShot()
{
    View v1 = ll;
    Bitmap bm = captureScreen(v1);
    String pat="";
    try {
        pat = saveImage(bm);
    }
    catch(IOException e)
    {

    }
    Uri temp = Uri.parse(pat);
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("image/jpeg");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, temp);
    startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
public static Bitmap captureScreen(View v) {

    Bitmap screenshot = null;
    try {

        if(v!=null) {

              screenshot=Bitmap.createBitmap(v.getMeasuredWidth(),v.getMeasuredHeight(),    Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(screenshot);
            v.draw(canvas);
        }

    }catch (Exception e){

     }

    return screenshot;
}

public static String saveImage(Bitmap bitmap) throws IOException {

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
    File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
    String path  = f.getAbsolutePath();
    f.createNewFile();
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
    fo.close();
    return path;
}