如何在android studio中拍摄一个linearlayout的图片? [英] how to take a picture of one linearlayout in android studio?

查看:33
本文介绍了如何在android studio中拍摄一个linearlayout的图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为活动子布局的一部分拍照.所以..活动本身将是FrameLayout而FrameLayout包含其他一些视图,例如工具栏,ImageView,TextView等.

I would like take a picture of a part of activity sub layout. so.. activity itself will be FrameLayout and FrameLayout includes some other views like Toolbar, ImageView, TextView etc..

我想在sd卡中制作JPEG图片,以便仅存储父级布局的子布局.

and I would like to make a JPEG picture into my sd card in order to store just a sublayout of parent layout.

我试图在Google上寻找方法,发现了一个在Android Studio中过时的解决方案

I have tried to look the way on google and I found a solution which has been obsolated in android studio

提前谢谢

推荐答案

您完成了完整的源代码.

You complete full source like..

 private void snapScreen() {
    View v1 = getWindow().getDecorView().getRootView();
    // View v1 = iv.getRootView(); //even this works
    View v1 = findViewById(R.id.main_result); //this works too for particular view
    // but gives only content
    Bitmap myBitmap;
    v1.setDrawingCacheEnabled(true);
    myBitmap = v1.getDrawingCache();
    snap.saveBitmap(myBitmap, IntentResultItem);
}


用于创建快照


For create Snapshot

确保您已授予外部存储权限

public class SendSnap {

Context mContext;

public SendSnap(Context context) {
    this.mContext = context;
}


public void saveBitmap(Bitmap bitmap, String msg) {

    String filePath = Environment.getExternalStorageDirectory()
            + File.separator + "screenshot.png";
    File imagePath = new File(filePath);
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

}

这篇关于如何在android studio中拍摄一个linearlayout的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆