当前位置:   android > 正文

如何在android中截取布局的截图

android,android-gallery,android-layout,android-studio,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成,unix时间戳,在线开发工具,前端开发工具,开发人员工具,站长工具

我想通过拍摄布局的屏幕截图将我的框架布局保存到图库中.这段代码适用于一个项目(名称:试用版),但当我在其他项目(名称:Fg)上尝试相同的代码时,它无效.照片未保存在图库中.

@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;


}

}



1> KoalaKoalifi..:

你有没有


在你的清单中声明?

此外,如果你想要整个视图试试这个:

view1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view1.getDrawingCache());

哦,这可能会节省你一些时间:

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);
}

Src:如何以编程方式在Android中截取屏幕截图?

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/android/detail/19397
推荐阅读
相关标签
  

闽ICP备14008679号