【发布时间】:2014-01-29 11:01:51
【问题描述】:
在这一行:BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 它会抛出一个 fileNotFound 异常。这是Logcat:
01-29 23:56:12.296: E/BitmapFactory(30046): Unable to decode stream: java.io.FileNotFoundException: /file:/storage/emulated/0/Pictures/JPEG_20140129_235544_1090805596.jpg: open failed: ENOENT (No such file or directory)
在 setPic();但是文件在启动意图期间被保存并添加到库中,所以在onActivityResult之前,所以它应该在那里。你看有什么问题吗?此代码取自 Android 开发者网站http://developer.android.com/training/camera/photobasics.html
static final int REQUEST_TAKE_PHOTO = 1001;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
EDIT下一个代码块来自 onActivityResult:
else if ((requestCode == REQUEST_TAKE_PHOTO) && (resultcode == -1)){
// Uri selectedImage = imageUri;
mProfilePicPath = mCurrentPhotoPath;
mPortraitPhoto = setPic();
TextView tv = (TextView) findViewById(id.ProfilePicText);
tv.setText(mProfilePicPath);
//}
// }
}
}catch(Exception ex){
Log.d("shkdghrfb", ex.toString());
}
}
String mCurrentPhotoPath;
private Bitmap setPic() {
// Get the dimensions of the View
// 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;
// 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 = 5;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
//mImageView.setImageBitmap(bitmap);
return bitmap;
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
galleryAddPic();
return image;
}
我已添加的清单权限:
<uses-permission android:name="android.permission.CAMERA" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
【问题讨论】:
-
这是关于您今天早上制作的相机的第三个问题,也许您应该尝试多想,少问....至少如果您当然想学习的话。
-
你是否在manifest文件中添加了读写外部存储权限?
-
@nsL 在今天之前我已经尝试了很多小时。在此之前,我尝试了许多不同的方法。这个问题已经给我带来了一个星期的麻烦。我做了一种不同的方式,可以在我的另一部手机上使用,但不是某些手机,所以不得不改变它。
-
@nsL 我是一名学生,从事自己制作应用程序的工作。
-
@PiyushGupta 我现在已将我的权限添加到问题的底部。谢谢。
标签: android android-intent android-camera