我在预览捕获的图片时遇到问题。
我已经设置了 ImageView 来预览 xml 文件中的图像。拍摄照片后,我会保存或丢弃拍摄图像的预览。
只有当我以横向屏幕模式拍摄照片并在 Android 清单文件中将方向声明为 android:screenOrientation="portrait" 时,我才能在 ImageView 中预览拍摄的照片。我可以在纵向视图的图像视图中预览图像!屏幕截图:横向屏幕模式:
但是,一旦以纵向屏幕模式捕获照片,单击“保存”按钮后,它不会在 ImageView 上显示任何预览。
截屏:
我正在使用三星 Glaxy Note II。
我试过这段代码:
public Bitmap decodeFile(String path) {//you can provide file path here
int orientation;
try {
if (path == null) {
return null;
}
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 0;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bm = BitmapFactory.decodeFile(path, o2);
Bitmap bitmap = bm;
ExifInterface exif = new ExifInterface(path);
orientation = exif
.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Log.e("ExifInteface .........", "rotation ="+orientation);
//exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90);
Log.e("orientation", "" + orientation);
Matrix m = new Matrix();
if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) {
m.postRotate(180);
//m.postScale((float) bm.getWidth(), (float) bm.getHeight());
// if(m.preRotate(90)){
Log.e("in orientation", "" + orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
return bitmap;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
m.postRotate(90);
Log.e("in orientation", "" + orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
return bitmap;
}
else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
m.postRotate(270);
Log.e("in orientation", "" + orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
return bitmap;
}
return bitmap;
} catch (Exception e) {
return null;
}
}
我的 xml 文件显示预览和执行其他功能:
<ImageView
android:id="@+id/ivReturnedPics"
android:layout_width="168dp"
android:layout_height="168dp"
android:layout_gravity="center"
android:src="@drawable/testsh"
android:padding="3dp" />
我正在使用代码: decodeFile(filePath); ivv.setImageBitmap(bmp);
提前感谢您的帮助。