我想要做的是使用相機意圖拍攝一張照片,記下URI(我將稍後使用此通過Firebase
存儲上傳圖像),旋轉圖像(如果需要),然後在ImageView
中顯示圖像。這就是我現在這樣做的情況,在AVD和Sony Xperia Z2
上運行的Marshmallow 6.0.1
都可以。但是,在運行Lollipop 5.0.1
的Samsung Galaxy S4
上進行測試時,我遇到了問題。代碼無法在指定的文件路徑中找到圖像。我也嘗試通過使用photoURI來設置ImageView
,並且我還嘗試在創建相機意圖時註釋額外信息,並通過data.getData()
獲取數據 - 這些方法都不起作用。我只需要一種方法就可以從該設備獲取此圖像,而不會崩潰,並且理想情況下不會影響設備兼容性。安卓相機意圖問題與一些設備
編輯:引導照相機意圖接管,都photoFilepath 和photoURI有價值。只要我得到
onActivityResult
, 都返回null。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 8;
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE){
try {
Bitmap bit = BitmapFactory.decodeFile(photoFilepath, opt);
Bitmap rotated = rotateImg(bit, photoFilepath);
userPhoto.setImageBitmap(rotated);
contentsOfImageView = rotated;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Error retrieving photo, please try again", Toast.LENGTH_LONG).show();
contentsOfImageView = null;
}
} // else if here for handling getting images from gallery
addBtn.setVisibility(View.INVISIBLE);
clearBtn.setVisibility(View.VISIBLE);
} else { // Result was a failure
//Toast.makeText(this, "Picture wasn't taken!", Toast.LENGTH_SHORT).show();
}
}
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
Log.d(TAG, ex.toString());
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"com.example.intheactualcodethisismypackagename",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
}
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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
photoFilepath = image.getAbsolutePath();
return image;
}
private Bitmap rotateImg(Bitmap before, String path) {
ExifInterface exif = null;
try {
exif = new ExifInterface(path);
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(270);
break;
default:
break;
}
return Bitmap.createBitmap(before, 0, 0, before.getWidth(), before.getHeight(), matrix, true);
}