首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法保存位图: ENOENT (没有这样的文件或目录)

无法保存位图: ENOENT (没有这样的文件或目录)
EN

Stack Overflow用户
提问于 2017-02-01 10:19:51
回答 4查看 1.3K关注 0票数 0

我试图保存位图,但是行outStream = new FileOutputStream(file);总是抛出错误java.io.FileNotFoundException: /storage/emulated/0/inpaint/card.png: open failed: ENOENT (No such file or directory)

代码语言:javascript
运行
复制
    //Save bitmap
    InputStream inputStream = null;
    try {
        inputStream = getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap bmp = BitmapFactory.decodeStream(inputStream);
    File f3=new File(Environment.getExternalStorageDirectory()+"/inpaint/");
    if(!f3.exists())
        f3.mkdirs();
    OutputStream outStream = null;
    File file = new File(Environment.getExternalStorageDirectory() + "/inpaint/"+"card"+".png");
    try {
        outStream = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, outStream);
        outStream.close();
        Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        e.printStackTrace();
    }

我还补充说

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

到舱单上。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-02-01 10:25:09

你可以应用以下代码-

代码语言:javascript
运行
复制
final int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
                                if (!checkIfAlreadyhavePermission()) {
                                   ActivityCompat.requestPermissions(YourActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                                } else {
                                    your_method();
                                }
                                } else {
                                    your_method();
                            }

private void your_code(){
      //Your entire code will go here
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    your_method();
                } else {
                    Toast.makeText(getApplicationContext(), "Please provide access to external storage", Toast.LENGTH_LONG).show();
                }
                break;
            }
   }
}
票数 1
EN

Stack Overflow用户

发布于 2017-02-01 10:40:18

如果您的设备包含棉花糖,则授予权限运行时:

代码语言:javascript
运行
复制
       private static final int PERMISSION_REQUEST_CODE = 1;
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_DENIED) {

            Log.d("permission", "permission denied to WRITE_EXTERNAL_STORAGE - requesting it");
            String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};

            requestPermissions(permissions, PERMISSION_REQUEST_CODE);

        }
    } 

在下面的方法中传递位图:

代码语言:javascript
运行
复制
          public void CaptureImage(Bitmap bitmap) {

    try {
        File imageFile;
        File dir;


        dir = new File(Environment.getExternalStorageDirectory(), "FolderName");

        boolean success = true;
        if (!dir.exists()) {
            success = dir.mkdirs();
        }
        if (success) {
            java.util.Date date = new java.util.Date();
            imageFile = new File(dir.getAbsolutePath()
                    + File.separator
                    + new java.sql.Timestamp(date.getTime()).toString()
                    + "Image.jpg");

            imageFile.createNewFile();
        } else {
            return;
        }
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();

        // save image into gallery
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);

        FileOutputStream fout = new FileOutputStream(imageFile);
        fout.write(ostream.toByteArray());
        fout.close();
        ContentValues values = new ContentValues();

        values.put(MediaStore.Images.Media.DATE_TAKEN,
                System.currentTimeMillis());
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.MediaColumns.DATA,
                imageFile.getAbsolutePath());

    } catch (Exception e) {
        e.printStackTrace();
    }
}
票数 3
EN

Stack Overflow用户

发布于 2017-02-01 10:28:02

我觉得你对我也有同样的问题。在android 23或更高版本中,您需要在代码中向用户请求额外的存储权限。详细说明在下面的链接。

http://www.androidhowtocode.com/2017/01/javaiofilenotfoundexception.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41977167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档