[英]Android: open failed: ENOENT (No such file or directory) Error
我正在按照本教程拍照,保存,縮放並在android中使用。 但是我正在獲取Android: open failed: ENOENT (No such file or directory)
嘗試打開/檢索保存的圖像時出現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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.