我使用OpenGL在Android手机中绘制框架,我想将图像保存到存储中,但是我收到错误:找不到目录,没有这样的文件目录,我在Android中放了权限表现如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.etoff.appsopengl">
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<application android:allowBackup="true" android:label="@string/app_name"
android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">
<activity
android:name="com.etoff.appsopengl.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这是我在舞台课上的编码:
if(SC==true){
int screenshotSize = screenWidth * screenHeight;
ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
bb.order(ByteOrder.nativeOrder());
gl.glReadPixels(0, 0, screenWidth, screenHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
int pixelsBuffer[] = new int[screenshotSize];
bb.asIntBuffer().get(pixelsBuffer);
bb = null;
Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.RGB_565);
bitmap.setPixels(pixelsBuffer, screenshotSize-screenWidth, -screenWidth, 0, 0, screenWidth, screenHeight);
pixelsBuffer = null;
short sBuffer[] = new short[screenshotSize];
ShortBuffer sb = ShortBuffer.wrap(sBuffer);
bitmap.copyPixelsToBuffer(sb);
for (int i = 0; i < screenshotSize; ++i) {
short v = sBuffer[i];
sBuffer[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
}
sb.rewind();
bitmap.copyPixelsFromBuffer(sb);
screen = bitmap;
String file_path = Environment.getExternalStorageDirectory().toString() + "/OpenGL";
File dir = new File(file_path);
if(!dir.exists()){
dir.mkdirs();
}
String format = new SimpleDateFormat("yyyyMMddHHmmss", java.util.Locale.getDefault()).format(new Date());
File file = new File(file_path, format + ".png");
OutputStream fOut;
try {
fOut = new FileOutputStream(file);
screen.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
SC = false;
}
这是我在控制台中遇到的错误:
W/System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/OpenGL/20151001152329.png: open failed: ENOENT (No such file or directory)
W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409)
W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
W/System.err﹕ at com.etoff.appsopengl.Stage$MyRenderer.onDrawFrame(Stage.java:167)
W/System.err﹕ at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1531)
W/System.err﹕ at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)
W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err﹕ at libcore.io.Posix.open(Native Method)
W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:393)
W/System.err﹕ ... 5 more
答案 0 :(得分:1)
错误:强>
引起:libcore.io.ErrnoException:open failed:ENOENT(不是这样的 文件或目录)libcore.io.Posix.open(本地方法)
基本上找不到您的图像文件,因为您提供的文件路径错误。
使用Environment.getExternalStorageDirectory()
获取SD卡路径。
删除.getAbsolutePath()
即可。 Environment.getExternalStoreDirectory()
将为您提供制造商设置外部存储空间的路径。
尝试
String file_path = Environment.getExternalStorageDirectory().toString()+"/OpenGL"
而不是
String file_path = Environment.getExternalStorageDirectory()+ "/OpenGL";
<强> EDIT1:强>
您需要添加此权限才能将文件写入内部存储空间。
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
我建议您将图像保存在内部存储而不是SD卡中,因为不同的制造商使用不同的SD卡名称,因此不同的设备有不同的SD卡路径。
<强> EDIT2:强>
试试此代码
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/OpenGL";
File dir = new File(file_path);
if(!dir.exists()){
dir.mkdirs();
}
String format = new SimpleDateFormat("yyyyMMddHHmmss", java.util.Locale.getDefault()).format(new Date());
File file = new File(file_path, format + ".png");
而不是
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/OpenGL";
File dir = new File(file_path);
if(!dir.exists()){
dir.mkdirs();
}
String format = new SimpleDateFormat("yyyyMMddHHmmss", java.util.Locale.getDefault()).format(new Date());
File file = new File(dir, format + ".png");
<强> EDIT3:强> 在清单文件中给予两个权限:
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
并尝试同时使用dir.mkdir();
和dir.mkdirs();
超过2个权限。
答案 1 :(得分:0)
您可以使用此方法保存图像位图以保存到SD卡
public static Uri saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
Uri parse = Uri.parse(new File(Environment.getExternalStorageDirectory() + "/screenshot.png").toString());
return parse;
}
注意 - 在清单中添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.STORAGE" />
享受您的代码时间:)