简体   繁体   English

libGDX:Android不保存截图

[英]libGDX: Android doesn't save screenshot

The problem is that when you click, for example, on the display, a screenshot of the screen should be saved, but this does not happen, the image simply is not in the gallery or elsewhere, while if you test for the desktop version, the screen is successfully saved.问题是,例如,当您在显示器上单击时,应该保存屏幕截图,但这并没有发生,图像根本不在图库或其他地方,而如果您测试桌面版本,屏幕已成功保存。 I am making a drawing for android with saving work, but I do not know how else to save a picture on android.我正在通过保存工作为 android 制作绘图,但我不知道如何在 android 上保存图片。

if (Gdx.input.isTouched())
{
    byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true);
    for (int i = 4; i < pixels.length; i += 4)
    {
        pixels[i - 1] = (byte) 255;
    }

    Pixmap pixmap = new Pixmap(Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), Pixmap.Format.RGBA8888);
    BufferUtils.copy(pixels, 0, pixmap.getPixels(), pixels.length);
    PixmapIO.writePNG(Gdx.files.local("mypixmap.png"), pixmap);
    local = Gdx.files.getExternalStoragePath().toString();
    System.out.println(Gdx.files.getLocalStoragePath());
    System.out.println(Gdx.files.getExternalStoragePath());
    pixmap.dispose();
}

Gdx.files.local is only for your internal files and can't be accessed by gallery. Gdx.files.local仅供您的内部文件使用,无法通过图库访问。

You need to use Gdx.files.external , but the current libGDX version uses a path here that is also not accessible under normal circumstances.您需要使用Gdx.files.external ,但是当前的 libGDX 版本在这里使用了一个在正常情况下也无法访问的路径。 It is changed for the next version . 它在下一个版本中被更改

Depending on what you want to achieve, there are other ways to get going with platform specific code.根据您想要实现的目标,还有其他方法可以使用特定于平台的代码。

  • Do you want to save screenshots periodically?您要定期保存屏幕截图吗? Use app-specific storage from 1.9.12从 1.9.12 开始使用特定于应用程序的存储
  • Do you want to share the screenshot to another app?您想将屏幕截图分享到其他应用程序吗? Use local storage and androidx.core.content.FileProvider使用本地存储和androidx.core.content.FileProvider
  • Do you want to open a dialog and the user can save the image?你想打开一个对话框,用户可以保存图像吗? Use Storage Access Framework使用存储访问框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM