【发布时间】:2021-02-25 07:01:32
【问题描述】:
我正在尝试截图,但是一直出现错误,我尝试了网站上建议的所有方法,我也在清单文件中写道:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
我也尝试使用方法:local(),getExternalStoragePath(),getLocalStoragePath()
这是我尝试编写文件的一种方式:
if (Gdx.input.isTouched() && !flag)
{
flag = true;
try
{
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);
FileHandle fh = Gdx.files.external("screenshot.png");
PixmapIO.writePNG(fh, pixmap);
System.out.println(fh.exists()); // return true
scaner.scanFile(fh);
pixmap.dispose();
} catch (Exception e)
{
e.printStackTrace();
}
我仍然得到错误
W/System.err: com.badlogic.gdx.utils.GdxRuntimeException: Error writing file: screenshot.png (External)
at com.badlogic.gdx.files.FileHandle.write(FileHandle.java:302)
at com.badlogic.gdx.graphics.PixmapIO$PNG.write(PixmapIO.java:222)
at com.badlogic.gdx.graphics.PixmapIO.writePNG(PixmapIO.java:67)
at com.badlogic.gdx.graphics.PixmapIO.writePNG(PixmapIO.java:79)
at com.repress.game.Start.render(Start.java:130)
at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:494)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1553)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1253)
Caused by: java.io.FileNotFoundException: /storage/emulated/0/screenshot.png (Permission denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at com.badlogic.gdx.files.FileHandle.write(FileHandle.java:298)
【问题讨论】: