Android:打开失败:ENOENT(没有这样的文件或目录)错误

问题描述:

我正在关注此tutorial拍照,保存,缩放和在Android中使用它。但是,当我尝试打开/检索保存的图像时,出现Android: open failed: ENOENT (No such file or directory)错误。经过一番研究之后,我发现这篇文章假设这个问题伴随着包含名字中的数字的文件,比如我的名字带有当前的时间戳。我检查了图像保存在文件目录中,并进行了日志记录以确保用于检索它们的文件名与原始名称匹配。 下面是给出了错误我的代码的一部分:Android:打开失败:ENOENT(没有这样的文件或目录)错误

private void setPic(ImageView myImageView) { 
    // Get the dimensions of the View 
    int targetW = myImageView.getWidth(); 
    int targetH = myImageView.getHeight(); 

    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 
    Log.v("IMG Size", "IMG Size= "+String.valueOf(photoW)+" X "+String.valueOf(photoH)); 

    // Determine how much to scale down the image 
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

    // Decode the image file into a Bitmap sized to fill the View 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    myImageView.setImageBitmap(bitmap); 
} 

这里是什么记录显示我:

E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /file:/storage/sdcard0/Pictures/JPEG_20150728_105000_1351557687.jpg: open failed: ENOENT (No such file or directory) 

,我试图打开一个名为图片:JPEG_20150728_105000_1351557687.jpg

+0

这可能意味着,在您使用不存在的完整路径的目录:创建(使用.mkdirs()和_DO不forget_检查返回值) – fge

+0

@DerGol ... LUM是一个完美有效的路径(当然,Windows文件系统除外)。当然,你可能想知道它实际上是否使用'file:/ ...'作为文件的URL,或者该方案是否完全在那里 – fge

+0

@fge好的,我会更好地问它:你是否存储你的图片**在这里**:'/ file:/ storage/sdcard0/...',在Android上? –

我下载了你的代码,并试图在我的应用程序中使用它。发现前缀/file:导致FileNotFoundException

将您的方法替换为以下方法。

private void setPic(ImageView myImageView) { 
    // Get the dimensions of the View 
    int targetW = myImageView.getWidth(); 
    int targetH = myImageView.getHeight(); 

    String path = mCurrentPhotoPath.replace("/file:",""); 

    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 
    Log.v("IMG Size", "IMG Size= " + String.valueOf(photoW) + " X " + String.valueOf(photoH)); 

    // Determine how much to scale down the image 
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

    // Decode the image file into a Bitmap sized to fill the View 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 

    Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions); 
    myImageView.setImageBitmap(bitmap); 
}