如何在android中捕获屏幕并将其转换为图像[重复]

Posted

技术标签:

【中文标题】如何在android中捕获屏幕并将其转换为图像[重复]【英文标题】:how to Capture screen in android and covert it to image [duplicate] 【发布时间】:2012-07-06 19:43:14 【问题描述】:

可能重复:Screenshot android

任何人请帮我解决这个问题..

如何以编程方式在android中捕获屏幕内容/.//

提前致谢..

【问题讨论】:

也检查一下:***.com/questions/3067586/… 看到这个问题***.com/questions/2661536/screenshot-android***.com/questions/3067586/…&这个也是techcrunch.com/2008/10/31/… 【参考方案1】:

试试这段代码,别忘了在你的 Manifest.xml 中添加这个权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/screen"
    android:layout_
    android:layout_
    android:orientation="vertical" >

    <TextView
        android:layout_
        android:layout_
        android:text="@string/hello" />

    <Button
        android:id="@+id/capturescreen"
        android:layout_
        android:layout_
        android:text="Capture Screen" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_
        android:layout_
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/roundcorner"
        android:scaleType="fitXY"
        android:src="@drawable/android_awesome" />

</RelativeLayout>

ScreenCaptureActivity.java

public class ScreenCaptureActivity extends Activity 

    Bitmap bmScreen;
    RelativeLayout mLayout;
    Dialog screenDialog;
    static final int ID_SCREENDIALOG = 1;

    ImageView bmImage;
    Button btnScreenDialog_OK;
    // TextView TextOut;

    View screen;
    EditText EditTextIn;

    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        screen = (View) findViewById(R.id.screen);
        Button btnCaptureScreen = (Button) findViewById(R.id.capturescreen);

        btnCaptureScreen.setOnClickListener(new OnClickListener() 

            public void onClick(View arg0) 
                // TODO Auto-generated method stub
                screen.setDrawingCacheEnabled(true);
                bmScreen = screen.getDrawingCache();
                saveImage(bmScreen);
                // showDialog(ID_SCREENDIALOG);
            
        );
    

    protected void saveImage(Bitmap bmScreen2) 
        // TODO Auto-generated method stub

        // String fname = "Upload.png";
        File saved_image_file = new File(
                Environment.getExternalStorageDirectory()
                        + "/captured_Bitmap.png");
        if (saved_image_file.exists())
            saved_image_file.delete();
        try 
            FileOutputStream out = new FileOutputStream(saved_image_file);
            bmScreen2.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();

         catch (Exception e) 
            e.printStackTrace();
        

    


【讨论】:

【参考方案2】:

因为您需要使用问题中描述的相同代码

// 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 = mCurrentUrlMask.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();
 

如本问题所述

How to programmatically take a screenshot in Android?

【讨论】:

以上是关于如何在android中捕获屏幕并将其转换为图像[重复]的主要内容,如果未能解决你的问题,请参考以下文章

将框架布局转换为图像并保存[关闭]

android捕获照片并将其保存在图像视图中并发送彩信?

如何单击图像按钮并将其显示在 Android Studio 的新页面中?

捕获图像并将其存储到 SD 卡的新文件夹中

以适当的格式创建 pdf

Android 捕获和显示图像应用程序