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

标签 java android io

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

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

的图像

最佳答案

我下载了您的代码并尝试在我的应用程序中使用相同的代码。发现前缀/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);
}

关于java - Android:打开失败:ENOENT(没有这样的文件或目录)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31671922/

相关文章:

java - 连接 Java 字符串中的连续整数

java - 使用给定的 JUNIT 测试查找数组中最接近平均值的值 ~

java - Java 中的 BMI 计算器

Android - 我的 ListPreference 中的自定义行布局

java - Activity 标签和多任务屏幕

java - 如何使用 xslt 将 XML 中的值附加到 TestNG 可通过电子邮件发送的 HTML 报告

Android Studio 2.2更新: aligned APK (zipAlign) not generated using the new Gradle Plugin 2. 2.0

java - 公开访问的 URL 抛出 IOException

c++ - 将端口作为变量传递 - AVR

c - 为什么 'EINTR' 未声明?