三星Glaxy NoteII Android应用程序中的纵向屏幕模式预览图像问题

三星Glaxy NoteII Android应用程序中的纵向屏幕模式预览图像问题,android,camera,preview,Android,Camera,Preview,我在预览拍摄的照片时遇到问题 我已经设置了ImageView来预览xml文件中的图像。拍摄完照片后,我将获得拍摄图像的保存或放弃预览 只有在以横向屏幕模式捕获照片并在android清单文件中将方向声明为android:screenOrientation=“纵向”时,我才能在ImageView中预览捕获的照片。我可以在纵向视图的图像视图中预览图像! 屏幕截图: 横向屏幕模式: 但是,在纵向屏幕模式下捕获照片后,单击“保存”按钮后,它不会在ImageView上显示任何预览 屏幕截图: 我使用的

我在预览拍摄的照片时遇到问题

我已经设置了ImageView来预览xml文件中的图像。拍摄完照片后,我将获得拍摄图像的保存或放弃预览

只有在以横向屏幕模式捕获照片并在android清单文件中将方向声明为android:screenOrientation=“纵向”时,我才能在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;
    }

}
公共位图解码文件(字符串路径){//您可以在此处提供文件路径
智力定向;
试一试{
if(路径==null){
返回null;
}
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
//找到正确的刻度值。它应该是2的幂。
所需的最终int_尺寸=70;
内部宽度=o.向外宽度,高度=o.向外高度;
整数比例=0;
while(true){
如果(宽度\u tmp/2<要求的\u尺寸
||高度(tmp/2<所需尺寸)
打破
宽度_tmp/=2;
高度_tmp/=2;
scale++;
}
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
位图bm=BitmapFactory.decodeFile(路径,o2);
位图=bm;
ExifInterface exif=新的ExifInterface(路径);
方向=exif
.getAttributeInt(ExifInterface.TAG_方向,1);
Log.e(“ExifInteface…………”、“rotation=“+方向”);
//setAttribute(ExifInterface.ORIENTATION\u ROTATE\u 90,90);
Log.e(“方向”,“方向+方向”);
矩阵m=新矩阵();
如果((方向==ExifInterface.orientation\u ROTATE\u 180)){
m、 旋转后(180);
//m、 postScale((float)bm.getWidth(),(float)bm.getHeight());
//如果(m.预旋转(90)){
Log.e(“方向内”,“方向+方向”);
bitmap=bitmap.createBitmap(bm,0,0,bm.getWidth(),
bm.getHeight(),m,true);
返回位图;
}else if(方向==ExifInterface.orientation\u ROTATE\u 90){
m、 旋转后(90);
Log.e(“方向内”,“方向+方向”);
bitmap=bitmap.createBitmap(bm,0,0,bm.getWidth(),
bm.getHeight(),m,true);
返回位图;
}
else if(方向==ExifInterface.orientation\u ROTATE\u 270){
m、 旋转后(270);
Log.e(“方向内”,“方向+方向”);
bitmap=bitmap.createBitmap(bm,0,0,bm.getWidth(),
bm.getHeight(),m,true);
返回位图;
} 
返回位图;
}捕获(例外e){
返回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" />

我正在使用代码: 解码文件(文件路径); 设置图像位图(bmp)


提前感谢您的帮助。

这对我来说很好,摘自facebook上的黑客手册

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(这是您的URI)
imageView.setImageBitmap(您的位图);
首先:

 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;  
                      }  
                      }  

这看起来像是Note2设备的一个bug。最新的Note3没有这个bug


感谢大家的贡献。

嗨,我是Boody,谢谢你们的回复。我使用的是intent。那么我如何将你们的逻辑集成到我的代码中呢?我已经看过你们使用scaleImage的示例实现(这个,你们的URI);这里,您使用了上下文上下文。如果没有上下文,我如何实现我的逻辑。因为我可以看到您的逻辑中有上下文对象。在我的代码中,当用户单击拍照按钮时,我使用的是意图。例如:case R.id.ibTakePic:i=new intent(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);startActivityForResult(i,camreaData);break;在活动结果上,我正在这样做:activityResult(int-requestCode,int-resultCode,Intent-data)上受保护的void{super.onActivityResult(requestCode,resultCode,data);if(resultCode==result_-OK){………..if(filePath!=null){decodeFile(filePath);}else{bmp=null;}您使用它是为了拍照吗?它是否需要与三星设备不同的方法?它需要从3.2到2.2的变通方法。如果您的应用程序可以在2.3.x上工作,那么您就必须使用它。只需回复我,我会发布代码。嗨,我是Obody。谢谢您的回复。很抱歉,回复太晚了。是的,如果您可以向我展示您的示例,我会的我要找出我的问题。thanks@techhunter看看我的答案。我很惊讶。谢谢。但这仍然不是我想要的解决方案:(