I'm using OpenGL to draw frame in Android phone and I want to save the image to the storage, but I get error: the directory is not found and no such file directory, I do put permission in the Android Manifest like this:
<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>
this is my coding in stage class:
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;
}
this is the error I get in the console:
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