我对捕获图片的预览有问题。
我已经设置了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(文件路径); ivv.setImageBitmap(BMP);
提前感谢您的帮助。
答案 0 :(得分:0)
这对我来说非常适合从Facebook的Hack Book中获取
public static Bitmap scaleImage(final Context context, final Uri photoUri)
throws IOException {
InputStream is = context.getContentResolver().openInputStream(photoUri);
final BitmapFactory.Options dbo = new BitmapFactory.Options();
dbo.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, dbo);
is.close();
int rotatedWidth, rotatedHeight;
final int orientation = getOrientation(context, photoUri);
if (orientation == 90 || orientation == 270) {
rotatedWidth = dbo.outHeight;
rotatedHeight = dbo.outWidth;
} else {
rotatedWidth = dbo.outWidth;
rotatedHeight = dbo.outHeight;
}
Bitmap srcBitmap;
is = context.getContentResolver().openInputStream(photoUri);
if (rotatedWidth > MAX_IMAGE_DIMENSION
|| rotatedHeight > MAX_IMAGE_DIMENSION) {
final float widthRatio = ((float) rotatedWidth)
/ ((float) MAX_IMAGE_DIMENSION);
final float heightRatio = ((float) rotatedHeight)
/ ((float) MAX_IMAGE_DIMENSION);
final float maxRatio = Math.max(widthRatio, heightRatio);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = (int) maxRatio;
srcBitmap = BitmapFactory.decodeStream(is, null, options);
} else {
srcBitmap = BitmapFactory.decodeStream(is);
}
is.close();
if (orientation > 0) {
final Matrix matrix = new Matrix();
matrix.postRotate(orientation);
srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0,
srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true);
}
final String type = context.getContentResolver().getType(photoUri);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (type.equals("image/png")) {
srcBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
} else if (type.equals("image/jpg") || type.equals("image/jpeg")) {
srcBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
}
final byte[] bMapArray = baos.toByteArray();
baos.close();
return BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
}
public static int getOrientation(final Context context, final Uri photoUri) {
/* it's on the external media. */
final Cursor cursor = context.getContentResolver().query(photoUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
null, null, null);
if (cursor.getCount() != 1) {
return -1;
}
cursor.moveToFirst();
return cursor.getInt(0);
}
在onActivityResult
中只需致电scaleImage(this,YOU_URI);
和imageView.setImageBitmap(Your_Bitmap);
答案 1 :(得分:0)
首先:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
努力工作第二。 onActivityResult
(requestCode == CAMERA_PIC_REQUEST) {
imageFileUri = intent.getData();
bmp = (Bitmap) intent.getExtras().get("data");
view.setImageBitmap(bmp);
view.setImageBitmap(bmp);// its weird i know but it wont work if not like this :D
if (imageFileUri != null) {
imageFileUri = intent.getData();
bmp = (Bitmap) intent.getExtras().get("data");
view.setImageBitmap(bmp);
}
else {
// Describe the columns you'd like to have returned.
// Selecting from the Thumbnails location gives you both the
// Thumbnail Image ID, as well as the original image ID
String[] projection = {
MediaStore.Images.Thumbnails._ID, // The columns we wANT
MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND,
MediaStore.Images.Thumbnails.DATA };
String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select
// only // mini's
MediaStore.Images.Thumbnails.MINI_KIND;
String sort = MediaStore.Images.Thumbnails._ID + " DESC";
// At the moment, this is a bit of a hack, as I'm returning
// ALL images, and just taking the latest one. There is a
// better way to narrow this down I think with a WHERE
// clause which is currently the selection variable
Cursor myCursor = this.managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, selection, null, sort);
long imageId = 0l;
long thumbnailImageId = 0l;
@SuppressWarnings("unused")
String thumbnailPath = "";
try {
myCursor.moveToFirst();
imageId = myCursor.getLong(myCursor .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
thumbnailImageId = myCursor.getLong(myCursor .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
thumbnailPath = myCursor
.getString(myCursor .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
} finally {
myCursor.close();
}
// Create new Cursor to obtain the file Path for the large
// image
String[] largeFileProjection = {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA };
String largeFileSort = MediaStore.Images.ImageColumns._ID
+ " DESC";
myCursor = this.managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
largeFileProjection, null, null, largeFileSort);
@SuppressWarnings("unused")
String largeImagePath = "";
try {
myCursor.moveToFirst();
// This will actually give yo uthe file path location of
// the image.
largeImagePath = myCursor
.getString(myCursor
.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
} finally {
myCursor.close();
}
// These are the two URI's you'll be interested in. They
// give you a handle to the actual images
Uri uriLargeImage = Uri.withAppendedPath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
String.valueOf(imageId));
@SuppressWarnings("unused")
Uri uriThumbnailImage = Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
String.valueOf(thumbnailImageId));
imageFileUri = uriLargeImage;
}
}
答案 2 :(得分:0)
它看起来像Note 2设备的错误。最新的Note 3没有这个bug。
谢谢你们的贡献。