gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:17:12 25 4
gpt4 key购买 nike

我正在关注这个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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com