由于我已将Android Studio 更新为 3.6.3,因此无法在该onActivityResult()
功能中拍照。我使用的是带有Android 8的三星手机,并且存储和相机权限设置为允许。
这是我的代码:
int CAMERA = 0;
int GALLERY = 1;
Uri imageURI;
File file;
public void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(), "image.jpg");
imageURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageURI);
startActivityForResult(intent, CAMERA);
}
// onActivityResult
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Bitmap bmp;
// • IMAGE FROM CAMERA
if (requestCode == CAMERA) {
try {
File f = file;
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { angle = 270; }
Matrix mat = new Matrix();
mat.postRotate(angle);
bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
assert bmp != null;
bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
bmp = scaleBitmapToMaxSize(800, bmp);
myImgView.setImageBitmap(bmp);
// error
} catch (IOException | OutOfMemoryError e) { Log.i("log-", "ERROR: " + e.getMessage()); }
}
}// ./ If RESULT_OK
}
// Scale image
public static Bitmap scaleBitmapToMaxSize(int maxSize, Bitmap bm) {
int outWidth;
int outHeight;
int inWidth = bm.getWidth();
int inHeight = bm.getHeight();
if (inWidth > inHeight) {
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
return Bitmap.createScaledBitmap(bm, outWidth, outHeight, false);
}
显现:
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA"/>
拍照后,我在 Logcat 中收到此错误,当然,在myImgView中没有显示图片:
ERROR: /storage/emulated/0/image.jpg: open failed: ENOENT (No such file or directory)
我怎样才能解决这个问题?