首先,我正在编写一个根应用程序,因此root权限不成问题.我搜索过并搜索过,发现很多代码对我来说从来没有用过,这是我到目前为止拼凑起来的东西.当我说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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何有关如何通过代码在Android屏幕截图拍摄的帮助将非常感谢谢谢!
===编辑===
以下是我正在使用的所有图像是在我的SD卡上制作的,不再是0bytes但是整个东西都是黑色的,上面没有任何内容.我已将活动绑定到我的搜索按钮,所以当我在手机上的某个地方时,我长按搜索,它应该拍摄屏幕截图但我只是得到一个黑色图像?一切都设置透明,所以我认为它应该抓住屏幕上的任何东西,但我只是一直变黑
表现
<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>
Run Code Online (Sandbox Code Playgroud)
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" …Run Code Online (Sandbox Code Playgroud) 我创建了一个位图,现在我想将该位图保存到某个目录.任何人都可以告诉我这是如何完成的.谢谢
FileInputStream in;
BufferedInputStream buf;
try {
in = new FileInputStream("/mnt/sdcard/dcim/Camera/2010-11-16_18-57-18_989.jpg");
buf = new BufferedInputStream(in);
Bitmap _bitmapPreScale = BitmapFactory.decodeStream(buf);
int oldWidth = _bitmapPreScale.getWidth();
int oldHeight = _bitmapPreScale.getHeight();
int newWidth = 2592;
int newHeight = 1936;
float scaleWidth = ((float) newWidth) / oldWidth;
float scaleHeight = ((float) newHeight) / oldHeight;
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
Bitmap _bitmapScaled = Bitmap.createBitmap(_bitmapPreScale, 0, 0, oldWidth, oldHeight, matrix, true);
Run Code Online (Sandbox Code Playgroud)
(我想将_bitmapScaled保存到SD卡上的文件夹中)