0

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
Le Parkour
  • 97
  • 1
  • 15

2 Answers2

1

Error:

Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Posix.open(Native Method)

basically your image file is not found because file path you have given is wrong.

Use Environment.getExternalStorageDirectory() to get Path of SD Card.

Remove the .getAbsolutePath() and it will be fine. Environment.getExternalStoreDirectory() will give you the path to wherever the manufacture has set their external storage.

Try

String file_path = Environment.getExternalStorageDirectory().toString()+"/OpenGL"

instead of

String file_path = Environment.getExternalStorageDirectory()+ "/OpenGL";

EDIT1:

You need to add this permission to write file into Internal storage.

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

I recommend you to save image in Internal Storage rather that SD card because Different manufacturer use different SD card name so there is different SD card path for different devices.

EDIT2:

Try this code

 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");

instead of

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: give both permission in manifest file:

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

and also try to use both dir.mkdir(); and dir.mkdirs(); with above 2 permissions.

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
0

you can use this method to save image bitmap to save into SD card

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;
}

note-Add permission in manifest:

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.STORAGE" />

enjoy your code time:)

John smith
  • 1,781
  • 17
  • 27
  • not work, this is the error Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Posix.open(Native Method) – Le Parkour Oct 01 '15 at 06:53