use*_*732 46 android caching screenshot image bitmap
首先,我正在编写一个根应用程序,因此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"
android:layout_height="match_parent"
android:background="#00000000"
android:id="@+id/screenRoot">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
截图类
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Jov*_*van 26
你去......我用过这个:
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();
}
Run Code Online (Sandbox Code Playgroud)
v iz root layout ...只是指向;)))
我认为你必须等到布局完全绘制..使用 ViewTreeObserver 在布局完全绘制时得到回调..
在 onCreate 上添加此代码..仅从 onGlobalLayout() 内部调用 getScreen..
ViewTreeObserver vto = content.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
content.getViewTreeObserver().removeGlobalOnLayoutListener(this);
getScreen();
}
});
Run Code Online (Sandbox Code Playgroud)
我曾经问过一个有点类似的问题..请参阅我的问题,它解释了在 android 中截取屏幕截图的方法..希望这有帮助
| 归档时间: |
|
| 查看次数: |
43903 次 |
| 最近记录: |