无法去code流java.io.FileNotFoundException /存储/模拟/ 0打开失败:ENOENT(没有这样的文件或目录 [英] unable to decode stream java.io.FileNotFoundException /storage/emulated/0 open failed:ENOENT(No such file or directory

查看:7283
本文介绍了无法去code流java.io.FileNotFoundException /存储/模拟/ 0打开失败:ENOENT(没有这样的文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是想救我的应用程序所拍摄的照片,但是当我尝试访问内存来放置数据,错误出来

无法去code流java.io.FileNotFoundException /存储/模拟/ 0打开失败:ENOENT(没有这样的文件或目录)

这是我的code。

  Camera.PictureCallback mPictureCallback =新Camera.PictureCallback(){        公共无效onPictureTaken(字节[]数据,相机摄像头){
            // TODO自动生成方法存根
            如果(数据!= NULL){
                //意图mIntent =新意图();
                //mIntent.putExtra(\"image\",imageData);                mCamera.stop preVIEW();
                米previewRunning = FALSE;
                mCamera.release();                 尝试{
                     BitmapFactory.Options选择采用=新BitmapFactory.Options();
                     位图位图= BitmapFactory.de codeByteArray的(数据,0,data.length,选择采用);
                     位图= Bitmap.createScaledBitmap(位图,300,300,假);
                     INT宽度= bitmap.getWidth();
                     INT高度= bitmap.getHeight();
                     INT newWidth = 300;
                     INT newHeight = 300;                     //计算比例 - 在这种情况下= 0.4f
                     浮动scaleWidth =((浮点)newWidth)/宽度;
                     浮动scaleHeight =((浮点)newHeight)/身高;                     //用于操作createa矩阵
                     字模=新的Matrix();
                     //调整位图
                     matrix.postScale(scaleWidth,scaleHeight);
                     //旋转位图
                     matrix.postRotate(-90);
                     位图resizedBitmap = Bitmap.createBitmap(位图,0,0,
                             宽度,高度,矩阵,真);
                     Camera_local_db.image.setImageBitmap(resizedBitmap);                 }赶上(例外五){
                     e.printStackTrace();
                 }
               // StoreByteImage(mContext,为imageData,50,ImageName);
                //的setResult(FOTO_MODE,mIntent);
                的setResult(585);
                完();
            }
        }
    };
    Camera.PictureCallback jpegCallback =新相机。 PictureCallback(){
    @覆盖
    公共无效onPictureTaken(字节[]数据,相机摄像头){
        文件dir_image2 =新的文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),dddd.jpg);
        dir_image2.mkdirs(); //再次选择输出文件夹照片(这就好比是SURFACEVIEW
                                //截图)
        如果(!dir_image2.mkdirs()){
            Log.e(TAG,目录没有创建);
        }
        文件TMPFILE =新的文件(dir_image2TempGhost.jpg); //创建文件路径中的
                        // dir_image2(见右图上)和其命名为TempGhost.jpg或其他任何
        尝试{//节能
            FOS的FileOutputStream =新的FileOutputStream(TMPFILE);
            fos.write(数据);
            fos.close();
            // grabImage();
        }赶上(FileNotFoundException异常五){
            Toast.makeText(getApplicationContext(),错误,Toast.LENGTH_LONG).show();
        }赶上(IOException异常五){
            Toast.makeText(getApplicationContext(),错误,Toast.LENGTH_LONG).show();
        }
        //字符串路径= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);档案文件=新的文件(路径,/+ dir_image2);
        //字符串路径=(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+
          //文件分割符+TempGhost.jpg); //&下; ---        BitmapFactory.Options选项=新BitmapFactory.Options(); //< ---
        options.in preferredConfig = Bitmap.Config.ARGB_8888; //< ---
        BMP1 = BitmapFactory.de codeFILE(tmpFile.toString(),选件); //< ---
        //线以上读取该文件之前我们保存,并把它转换成位图
        Camera_local_db.image.setImageBitmap(BMP1);
        //camera_image.setImageBitmap(bmp1); //设定为图像位图在ImageView的(SOMETHING
                                    //像一个BACKGROUNG的布局)
       // TakeScreenshot(); //调用此方法获取屏幕截图    }
    };


解决方案

您需要编写到外部存储,请确保您添加的权限:

 <清单...>
    <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
    ...
< /清单>

检查是否外部存储可用于读取和写入:

 公共布尔isExternalStorageWritable(){
    字符串状态= Environment.getExternalStorageState();
    如果(Environment.MEDIA_MOUNTED.equals(州)){
        返回true;
    }
    返回false;
}

使用公共目录的根目录下,而不是使用Android的根。

如果你想保存在外部存储公用文件,请使用getExternalStoragePublicDirectory()

 公开文件getAlbumStorageDir(字符串ALBUMNAME){
    //获取用户的公开的图片目录。
    档案文件=新的文件(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DCIM),ALBUMNAME);
    如果(!file.mkdirs()){
        Log.e(LOG_TAG,目录没有创建);
    }
    返回文件;
}

如果您想要保存仅对您的应用程序文件,使用getExternalFilesDir()

 公开文件getAlbumStorageDir(上下文的背景下,字符串ALBUMNAME){
    //获取应用程序的私人照片目录。
    档案文件=新的文件(context.getExternalFilesDir(
            Environment.DIRECTORY_DCIM),ALBUMNAME);
    如果(!file.mkdirs()){
        Log.e(LOG_TAG,目录没有创建);
    }
    返回文件;
}

链接的更多信息 http://developer.android.com /training/basics/data-storage/files.html

hello i'm trying to save pictures taken on my application, but when i try to access the memory to place the data, an error comes out

unable to decode stream java.io.FileNotFoundException /storage/emulated/0 open failed:ENOENT(No such file or directory)

this is my code.

            Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
            if (data != null){
                //Intent mIntent = new Intent();
                //mIntent.putExtra("image",imageData);

                mCamera.stopPreview();
                mPreviewRunning = false;
                mCamera.release();

                 try{
                     BitmapFactory.Options opts = new BitmapFactory.Options();
                     Bitmap bitmap= BitmapFactory.decodeByteArray(data, 0, data.length,opts);
                     bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, false);
                     int width = bitmap.getWidth();
                     int height = bitmap.getHeight();
                     int newWidth = 300;
                     int newHeight = 300;

                     // calculate the scale - in this case = 0.4f
                     float scaleWidth = ((float) newWidth) / width;
                     float scaleHeight = ((float) newHeight) / height;

                     // createa matrix for the manipulation
                     Matrix matrix = new Matrix();
                     // resize the bit map
                     matrix.postScale(scaleWidth, scaleHeight);
                     // rotate the Bitmap
                     matrix.postRotate(-90);
                     Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                             width, height, matrix, true);
                     Camera_local_db.image.setImageBitmap(resizedBitmap);

                 }catch(Exception e){
                     e.printStackTrace();
                 }
               // StoreByteImage(mContext, imageData, 50,"ImageName");
                //setResult(FOTO_MODE, mIntent);
                setResult(585);
                finish();
            }       
        }
    };


    Camera.PictureCallback jpegCallback = new Camera. PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {


        File dir_image2 = new  File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"dddd.jpg");
        dir_image2.mkdirs();  //AGAIN CHOOSING FOLDER FOR THE PICTURE(WHICH IS LIKE A SURFACEVIEW 
                                //SCREENSHOT)
        if (!dir_image2.mkdirs()) {
            Log.e(TAG, "Directory not created");
        }


        File tmpFile = new File(dir_image2,"TempGhost.jpg"); //MAKING A FILE IN THE PATH                 
                        //dir_image2(SEE RIGHT ABOVE) AND NAMING IT "TempGhost.jpg" OR ANYTHING ELSE
        try {//SAVING
            FileOutputStream fos = new FileOutputStream(tmpFile);
            fos.write(data);
            fos.close();
            //grabImage();
        } catch (FileNotFoundException e) {
            Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
        }
        //String path = Environment.getExternalStoragePublicDirectory(    Environment.DIRECTORY_MOVIES); File file = new File(path, "/" + dir_image2);
        //String path = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)+  
          //      File.separator+"TempGhost.jpg");//<---

        BitmapFactory.Options options = new BitmapFactory.Options();//<---
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;//<---
        bmp1 = BitmapFactory.decodeFile(tmpFile.toString(), options);//<---
        //THE LINES ABOVE READ THE FILE WE SAVED BEFORE AND CONVERT IT INTO A BitMap
        Camera_local_db.image.setImageBitmap(bmp1);
        //camera_image.setImageBitmap(bmp1); //SETTING THE BitMap AS IMAGE IN AN IMAGEVIEW(SOMETHING
                                    //LIKE A BACKGROUNG FOR THE LAYOUT)
       // TakeScreenshot();//CALLING THIS METHOD TO TAKE A SCREENSHOT

    }
    };

解决方案

You need to write to external storage, make sure you added the permission:

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

Checks if external storage is available for read and write:

public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

Use the root of the public directory instead of using the root of Android.

If you want to save public files on the external storage, use the getExternalStoragePublicDirectory()

public File getAlbumStorageDir(String albumName) {
    // Get the directory for the user's public pictures directory. 
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DCIM), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}

If you want to save files that are private to your app, use the getExternalFilesDir()

public File getAlbumStorageDir(Context context, String albumName) {
    // Get the directory for the app's private pictures directory. 
    File file = new File(context.getExternalFilesDir(
            Environment.DIRECTORY_DCIM), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}

More information on the link http://developer.android.com/training/basics/data-storage/files.html

这篇关于无法去code流java.io.FileNotFoundException /存储/模拟/ 0打开失败:ENOENT(没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆