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

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

问题描述

你好我正在尝试保存在我的应用程序中拍摄的照片,但是当我尝试访问内存以放置数据时,会出现错误。



无法解码流java.io.FileNotFoundException / storage / emulated / 0打开失败:ENOENT(没有这样的文件或目录)



这是我的代码

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

public void onPictureTaken(byte [] data,Camera camera) {
// TODO自动生成的方法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;

//计算比例 - 在这种情况下= 0.4f
float scaleWidth =((float)newWidth)/ width;
float scaleHeight =((float)newHeight)/ height;

// createa矩阵的操作
矩阵矩阵=新的Matrix();
//调整位图的大小
matrix.postScale(scaleWidth,scaleHeight);
//旋转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 =新相机。 PictureCallback(){
@Override
public void onPictureTaken(byte [] data,Camera camera){


文件dir_image2 = new File(Environment.getExternalStoragePublicDirectory .DIRECTORY_DCIM), dddd.jpg);
dir_image2.mkdirs(); //为了图像选择文件夹(像SURFACEVIEW
// SCREENSHOT)
if(!dir_image2.mkdirs()){
Log.e(TAG,Directory not created );
}


文件tmpFile = new File(dir_image2,TempGhost.jpg); //在PATH中创建一个文件
// dir_image2(见上),并命名为TempGhost.jpg或任何ELSE
尝试{// 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); //< ---
//上面的线程读取我们之前保存的文件,并将其转换为BitMap
Camera_local_db.image.setImageBitmap(bmp1);
//camera_image.setImageBitmap(bmp1); //设置BitMap作为IMAGE IN IMAGEVIEW中的图像(SOMETHING
//类似于布局的BACKGROUNG)
// TakeScreenshot(); //调用此方法来获取屏幕

}
};


解决方案

您需要写入外部存储,确保添加了权限:

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

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

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

使用公共目录的根,而不是使用Android的根。 / p>

如果要将公用文件保存在外部存储上,请使用getExternalStoragePublicDirectory()

 code> public File getAlbumStorageDir(String albumName){
//获取用户公共图片目录的目录。
文件文件= new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM),albumName);
if(!file.mkdirs()){
Log.e(LOG_TAG,Directory not created);
}
返回文件;
}

如果要保存您的应用程序私有的文件,请使用getExternalFilesDir ()

  public File getAlbumStorageDir(Context context,String albumName){
//获取应用程序私有的目录图片目录。
文件文件= new File(context.getExternalFilesDir(
Environment.DIRECTORY_DCIM),albumName);
if(!file.mkdirs()){
Log.e(LOG_TAG,Directory not created);
}
返回文件;
}

有关链接的更多信息 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

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

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