【问题标题】:Android unable to save image file to SD cardAndroid无法将图像文件保存到SD卡
【发布时间】:2015-10-01 04:38:52
【问题描述】:

我正在使用 OpenGL 在 Android 手机中绘制框架,我想将图像保存到存储中,但我收到错误:找不到目录并且没有这样的文件目录,我确实在 Android Manifest 中设置了权限,例如这个:

<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

【问题讨论】:

标签: android image save


【解决方案1】:

错误:

原因:libcore.io.ErrnoException: open failed: ENOENT (No such 文件或目录)在 libcore.io.Posix.open(Native Method)

基本上找不到您的图像文件,因为您提供的文件路径错误。

使用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个权限。

【讨论】:

  • 好的,先生,我尝试使用内部存储
  • String file_path = Environment.getDataDirectory().toString() + "/OpenGL";我更新了我的代码,但我仍然有同样的错误
  • 您是否添加了 权限?请在您的问题中发布更新的代码
  • 也试试这个 OutputStream fOut = new FileOutputStream(file);而不是 FileOutputStream fOut = new FileOutputStream(file);
  • 使用文件 file = new File(file_path, format + ".png");而是 File file = new File(dir, format + ".png");
【解决方案2】:

您可以使用此方法将图像位图保存到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" />

享受你的代码时间:)

【讨论】:

  • 不工作,这是错误原因:libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Posix.open(Native Method)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多