采取截图使用原生的Android API [英] Taking Screenshot using native android API
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/5973974/how-to-take-snapshot-of-screen-programmatically\">how采取屏幕编程的快照?
作为建设我的新应用程序的一部分,我想允许用户把我的应用程序使用按钮设备的屏幕截图。
不过,我需要在不生根的设备,使用本机API完成这件事。
As part of building my new app, I would like to allow the user to take a screenshot of the device using a button in my app. However I need this done without rooting of the device, using native API.
我试图寻找引用和Android开发者网站,但找不到原生API功能上面做。
I was trying to look for references and android developer site, but couldn't find the native API functionality to do the above.
能否请你确认它的确可以做到以上(以截图编程)?
能否请您也告诉我在什么地方我可以看到API?
Can you please confirm that it is indeed possible to do the above (Taking a screenshot programmatically)? Can you please also direct me to somewhere I can see the API at?
推荐答案
试试这个code:
public class ScreenShotActivity 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();
}
}
}
添加过多的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
XML这将是:
XML for this will be:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/capturescreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Capture Screen" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/roundcorner"
android:scaleType="fitXY"
android:src="@drawable/android_awesome" />
</RelativeLayout>
这篇关于采取截图使用原生的Android API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!