Android以编程方式拍摄屏幕截图
首先我写一个根应用程序,所以根权限是没有问题的。 我search和search,发现了很多从未为我工作的代码,这是我拼凑到目前为止,还有一些作品。 当我说sorta我的意思是它使我/ sdcard / test.png上的图像,但该文件是0字节,显然无法查看。
public class ScreenShot extends Activity{ View content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.blank); content = findViewById(R.id.blankview); getScreen(); } private void getScreen(){ Bitmap bitmap = content.getDrawingCache(); File file = new File("/sdcard/test.png"); try { file.createNewFile(); FileOutputStream ostream = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 100, ostream); ostream.close(); } catch (Exception e) { e.printStackTrace(); } } }
任何帮助,我可以通过代码在Android的屏幕截图将不胜感激,谢谢!
===编辑===
以下是我使用的所有图像是在我的SD卡上,不再是0bytes,但整个事情是黑色的,没有任何东西。 我已经将活动绑定到我的searchbutton,所以当我在我的手机上的某个地方长按search,它应该采取一个屏幕截图,但我只是得到一个黑色的图像? 一切都设置为透明,所以我认为它应该抓住屏幕上的任何东西,但我只是不断变黑
performance
<activity android:name=".extras.ScreenShot" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:noHistory="true" > <intent-filter> <action android:name="android.intent.action.SEARCH_LONG_PRESS" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000" android:id="@+id/screenRoot"> </LinearLayout>
截图类
public class ScreenShot extends Activity{ View content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screenshot); content = findViewById(R.id.screenRoot); ViewTreeObserver vto = content.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { content.getViewTreeObserver().removeGlobalOnLayoutListener(this); getScreen(); } }); } private void getScreen(){ View view = content; View v = view.getRootView(); v.setDrawingCacheEnabled(true); Bitmap b = v.getDrawingCache(); String extr = Environment.getExternalStorageDirectory().toString(); File myPath = new File(extr, "test.jpg"); FileOutputStream fos = null; try { fos = new FileOutputStream(myPath); b.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen"); }catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finish(); } }
在这里,你去…我用这个:
View v = view.getRootView(); v.setDrawingCacheEnabled(true); Bitmap b = v.getDrawingCache(); String extr = Environment.getExternalStorageDirectory().toString(); File myPath = new File(extr, getString(R.string.free_tiket)+".jpg"); FileOutputStream fos = null; try { fos = new FileOutputStream(myPath); b.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); MediaStore.Images.Media.insertImage( getContentResolver(), b, "Screen", "screen"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
viz根布局…只是指向;)))
对于这个问题的下一个读者来说,
通过绘制视图到canvas的非常简单的方法 –
把你的主布局引用传给这个方法 –
Bitmap file = save(layout); Bitmap save(View v) { Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.draw(c); return b; }
我认为你必须等到布局完全绘制完成后。使用ViewTreeObserver在布局完全绘制时得到callback。
在您的onCreate添加此代码..只需从内部onGlobalLayout()调用getScreen ..
ViewTreeObserver vto = content.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { content.getViewTreeObserver().removeGlobalOnLayoutListener(this); getScreen(); } });
我问了一个有点类似的问题一次..请看看我的问题,这解释了如何在android中截图的方式..希望这有助于
public class MainActivity extends Activity { Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Bitmap bitmap = takeScreenshot(); saveBitmap(bitmap); } }); } public Bitmap takeScreenshot() { View rootView = findViewById(android.R.id.content).getRootView(); rootView.setDrawingCacheEnabled(true); return rootView.getDrawingCache(); } 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); fos.flush(); fos.close(); } catch (FileNotFoundException e) { Log.e("GREC", e.getMessage(), e); } catch (IOException e) { Log.e("GREC", e.getMessage(), e); } } }
不要忘记给写外部存储权限!
你采取这样的屏幕截图……..
View view = findViewById(R.id.rellayout); view.getRootView(); String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { File picDir = new File(Environment.getExternalStorageDirectory()+ "/name"); if (!picDir.exists()) { picDir.mkdir(); } view.setDrawingCacheEnabled(true); view.buildDrawingCache(true); Bitmap bitmap = view.getDrawingCache(); // Date date = new Date(); String fileName = "mylove" + ".jpg"; File picFile = new File(picDir + "/" + fileName); try { picFile.createNewFile(); FileOutputStream picOut = new FileOutputStream(picFile); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), (int)(bitmap.getHeight()/1.2));//Optional boolean saved = bitmap.compress(CompressFormat.JPEG, 100, picOut); if (saved) { Toast.makeText(getApplicationContext(), "Image saved to your device Pictures "+ "directory!", Toast.LENGTH_SHORT).show(); } else { //Error } picOut.close(); } catch (Exception e) { e.printStackTrace(); } view.destroyDrawingCache(); } else { }
我的问题是“TargetApi(23)”,如果你的minSdkVersion低于23,就需要这个。
所以,我有以下代码段的请求权限
protected boolean shouldAskPermissions() { return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1); } @TargetApi(23) protected void askPermissions() { String[] permissions = { "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE" }; int requestCode = 200; requestPermissions(permissions, requestCode); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // ... if (shouldAskPermissions()) { askPermissions(); } }