libGDX: Android无法保存截屏

huangapple go评论64阅读模式
英文:

libGDX: Android doesn't save screenshot

问题

问题是,当您点击例如显示屏时,应该保存屏幕截图,但实际上并未发生这种情况,图像根本不在图库或其他任何地方,而如果您测试桌面版本,则屏幕会成功保存。我正在制作一个用于 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();
}
英文:

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.

if (Gdx.input.isTouched())
{
    byte[] pixels = ScreenUtils.getFrameBufferPixels(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true);
    for (int i = 4; i &lt; 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(&quot;mypixmap.png&quot;), pixmap);
    local = Gdx.files.getExternalStoragePath().toString();
    System.out.println(Gdx.files.getLocalStoragePath());
    System.out.println(Gdx.files.getExternalStoragePath());
    pixmap.dispose();
}

答案1

得分: 0

Gdx.files.local仅适用于您的内部文件,无法被图库访问。

您需要使用Gdx.files.external,但当前的libGDX版本在此处使用的路径在正常情况下也无法访问。已在下一个版本中进行了更改

根据您想要实现的目标,有其他途径可以使用特定于平台的代码。

  • 您想要定期保存屏幕截图吗?从1.9.12开始使用应用程序特定存储。

  • 您想要将屏幕截图共享到另一个应用程序吗?使用本地存储和androidx.core.content.FileProvider

  • 您想要打开对话框,让用户可以保存图像吗?使用存储访问框架(Storage Access Framework)。

英文:

Gdx.files.local is only for your internal files and can't be accessed by gallery.

You need to use Gdx.files.external, but the current libGDX version uses a path here that is also not accessible under normal circumstances. 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

  • Do you want to share the screenshot to another app? Use local storage and androidx.core.content.FileProvider

  • Do you want to open a dialog and the user can save the image? Use Storage Access Framework

huangapple
  • 本文由 发表于 2020年8月19日 21:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63488672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定