写入文件时出错:screenshot.png(外部)
Error writing file: screenshot.png (External)
我正在尝试截屏,但一直出现错误,我尝试了网站上建议的所有方法,我还在清单文件中写道:
<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)
您必须在 java class 中授予运行时权限
在 oncreate 方法中调用此函数:
requestStoragePermission()
并在 onCreate 之外定义以下方法
//请求权限
private void requestStoragePermission() {
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) || (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED))
return;
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
//If the user has denied the permission previously your code will come to this block
//Here you can explain why you need this permission
//Explain here why you need this permission
}
//And finally ask for the permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
//当用户点击允许或拒绝时将调用此方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//Checking the request code of our request
if (requestCode == STORAGE_PERMISSION_CODE) {
//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Displaying a toast
Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
} else {
//Displaying another toast if permission is not granted
Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
}
}
}
我建议您使用以下代码将视图的图片拍摄为位图:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
然后写入外存。
最重要的是 android 在 android 10.In 中更改我们的隐私政策 10.In 如果没有 permission.But 您不会在外部存储中写入数据,别担心,将此标签添加到你的清单文件
requestlegacyexternalstorage="true"
我还是会回答我自己的问题。
为了授予用户写入和读取文件的权限,我检查用户是否授予了外部存储的权限,并请求权限。
感谢这个人:HERE
我正在尝试截屏,但一直出现错误,我尝试了网站上建议的所有方法,我还在清单文件中写道:
<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)
您必须在 java class 中授予运行时权限 在 oncreate 方法中调用此函数: requestStoragePermission()
并在 onCreate 之外定义以下方法
//请求权限
private void requestStoragePermission() {
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) || (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED))
return;
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
//If the user has denied the permission previously your code will come to this block
//Here you can explain why you need this permission
//Explain here why you need this permission
}
//And finally ask for the permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
//当用户点击允许或拒绝时将调用此方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//Checking the request code of our request
if (requestCode == STORAGE_PERMISSION_CODE) {
//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Displaying a toast
Toast.makeText(this, "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
} else {
//Displaying another toast if permission is not granted
Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show();
}
}
}
我建议您使用以下代码将视图的图片拍摄为位图:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
然后写入外存。 最重要的是 android 在 android 10.In 中更改我们的隐私政策 10.In 如果没有 permission.But 您不会在外部存储中写入数据,别担心,将此标签添加到你的清单文件
requestlegacyexternalstorage="true"
我还是会回答我自己的问题。 为了授予用户写入和读取文件的权限,我检查用户是否授予了外部存储的权限,并请求权限。 感谢这个人:HERE