[英]Android take screen shot programmatically
First off i am writing a root app so root permissions are no issue. 首先,我正在编写一个根应用程序,因此root权限不成问题。 I've searched and searched and found a lot of code that never worked for me here is what i've pieced together so far and sorta works.
我搜索过并搜索过,发现很多代码对我来说从来没有用过,这是我到目前为止拼凑起来的东西。 When i say sorta i mean it makes an image on my /sdcard/test.png however the file is 0 bytes and obviously can't be viewed.
当我说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();
}
}
}
Any help on how i can take a screen shot in android via code would be greatly appreciated thank you! 任何有关如何通过代码在Android屏幕截图拍摄的帮助将非常感谢谢谢!
===EDIT=== ===编辑===
The following is everything i'm using the image is made on my sdcard and is no longer 0bytes but the entire thing is black there is nothing on it. 以下是我正在使用的所有图像是在我的SD卡上制作的,不再是0bytes但是整个东西都是黑色的,上面没有任何内容。 I've bound the activity to my search button so when i'm some where on my phone i long press search and it is supposed to take a screen shot but i just get a black image?
我已将活动绑定到我的搜索按钮,所以当我在手机上的某个地方时,我长按搜索,它应该拍摄屏幕截图但我只是得到一个黑色图像? Everything is set transparent so i'd think it should grab whatever is on the screen but i just keep getting black
一切都设置透明,所以我认为它应该抓住屏幕上的任何东西,但我只是一直变黑
Manifest 表现
<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
<?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>
Screenshot class 截图类
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();
}
}
Here you go...I used this: 你去......我用过这个:
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();
}
v iz root layout...just to point ;))) v iz root layout ...只是指向;)))
For the next reader of this question- 对于这个问题的下一位读者 -
The very simple way to do this by drawing your view to canvas- 通过将您的视图绘制到画布来实现此目的的非常简单的方法 -
pass your main layout reference to this method- 将您的主要布局参考传递给此方法 -
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;
}
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);
}
}
}
don't forgot to give write external storage permission! 别忘了给外部存储写入权限!
I think you have to wait until the layout is drawn completely..Use ViewTreeObserver to get a call back when layout is drawn completely.. 我认为你必须等到布局完全绘制完毕。使用ViewTreeObserver在完全绘制布局时获得回调。
On your onCreate add this code..Only call getScreen from inside onGlobalLayout().. 在onCreate上添加此代码。只需从onGlobalLayout()内部调用getScreen ..
ViewTreeObserver vto = content.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
content.getViewTreeObserver().removeGlobalOnLayoutListener(this);
getScreen();
}
});
I asked a somewhat similiar question once..Please see my question which explains the way to take screenshot in android..Hope this helps 我曾经问了一个类似的问题 ..请看我的问题解释了在android中截取屏幕截图的方法。希望这有帮助
You Take Screen Shot Like this........ 你拍这样的屏幕截图........
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 {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.