如何在Android中获取布局的屏幕截图 [英] How to take screenshot of a layout in android
本文介绍了如何在Android中获取布局的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过拍摄布局的屏幕快照将框架布局保存到图库中.这段代码适用于一个项目(名称:Trial),但是当我在该项目上尝试相同的代码(名称:Fg)时,它不起作用.该照片未保存在图库中.
I want to save my frame layout into gallery by taking the screen shot of the layout. This code works on a project(Name: Trial), but when I tried the same code on othe project(Name: Fg), It did not work. The photo is not being saved in the Gallery.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_background);
}
public Bitmap click(View view) {
FrameLayout memecontentView =(FrameLayout) findViewById(R.id.my);
View view1 = memecontentView;
Bitmap b = Bitmap.createBitmap(view1.getWidth(), view1.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
view1.draw(canvas);
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();
}
return b;
}
}
推荐答案
您有
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在您的清单中声明了吗?
Declared in your manifest?
如果您希望整个视图都尝试此操作,请执行以下操作:
Also if you want the entire view try this:
view1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view1.getDrawingCache());
哦,这可能为您节省一些时间:
Oh and this may save you some time:
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
这篇关于如何在Android中获取布局的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文