【问题标题】:How to take screenshot of a layout in android如何在android中截取布局的屏幕截图
【发布时间】:2016-03-30 09:41:39
【问题描述】:

我想通过拍摄布局的屏幕截图将我的框架布局保存到图库中。此代码适用于一个项目(名称:试用),但是当我在其他项目(名称: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;


}

}

【问题讨论】:

标签: android android-layout android-studio android-gallery


【解决方案1】:

你买了吗

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在您的清单中声明?

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

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

源:How to programmatically take a screenshot in Android?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 2017-10-09
    相关资源
    最近更新 更多