前置摄像头拍摄时显示的图像,但后置摄像头拍摄时不可见的图像

Image shown when taken by front camera but not visible when taken by Back camera

我在我的应用程序中使用原生相机。拍完照片后,我会在 Imageview 的下一个 activity 上向用户展示它。现在的问题是,当我保存前置摄像头拍摄的照片时,图片显示在下一个 activity 的 imageview 中,但后置摄像头拍摄的照片却没有。

拍照后我要去下一个activity按以下方式:

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);

         if (resultCode == RESULT_OK) {


                 case REQUEST_CODE_HIGH_QUALITY_IMAGE:
                     Toast.makeText(getApplicationContext(),
                        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                                         Uri.parse("file://"
                                                         + Environment.getExternalStorageDirectory())));
                         //refreshing gallery
                         Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                         mediaScanIntent.setData(mHighQualityImageUri);
                         sendBroadcast(mediaScanIntent);

                         Intent intentActivity = new Intent(MyCameraActivity.this,PhotoSortrActivity.class);
                        intentActivity.putExtra("data", mHighQualityImageUri);
                        Log.v("Uri before Sending",mHighQualityImageUri+"");
                         startActivity(intentActivity);


                         break;
                 default:
                         break;
                 }

         }

这是我展示拍摄图像的地方。 :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photosortr);
        this.setTitle(R.string.instructions);
    image = (ImageView) findViewById(R.id.img_view);


    InputStream iStream = null;
    try {
        iStream = getContentResolver().openInputStream(uri);
         inputData = getBytes(iStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap cameraBitmap = BitmapFactory.decodeByteArray(inputData, 0, inputData.length);

    Bitmap cameraScaledBitmap = Bitmap.createScaledBitmap(cameraBitmap, cameraBitmap.getWidth(), cameraBitmap.getHeight(), true);
    Matrix matrix = new Matrix();
    if(cameraScaledBitmap.getWidth()>cameraScaledBitmap.getHeight())
    {
        matrix = new Matrix();
        matrix.postRotate(270);
    }
 //   final Bitmap newImage = Bitmap.createBitmap(cameraScaledBitmap.getWidth(), cameraScaledBitmap.getHeight(), Bitmap.Config.ARGB_8888);

    // ask the bitmap factory not to scale the loaded bitmaps
    BitmapFactory.Options opts = new BitmapFactory.Options();

    opts.inScaled = false;
   Bitmap cameraScaledBitmap2 = Bitmap.createBitmap(cameraScaledBitmap, 0, 0, cameraScaledBitmap.getWidth(), cameraScaledBitmap.getHeight(), matrix, true);
  //  image.setImageURI(uri);
    image.setImageBitmap(cameraScaledBitmap2);
    BitmapDrawable bg = new BitmapDrawable(cameraScaledBitmap2);

   // photoSorter.SetBackgroundFromUrl(data);

}

@Override
protected void onResume() {
    super.onResume();
    //photoSorter.loadImages(this);
}

@Override
protected void onPause() {
    super.onPause();
    //photoSorter.unloadImages();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
        //photoSorter.trackballClicked();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


public byte[] getBytes(InputStream inputStream) throws IOException {
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    int len = 0;
    while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
    }
    return byteBuffer.toByteArray();
}

这是我的第二个布局 activity:

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/fl_camera">


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        >



        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:contentDescription="content_desc_overlay"
            android:src="@drawable/ic_launcher"
            android:id="@+id/img_view"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            />
    </RelativeLayout>

</FrameLayout>

为什么在使用后置摄像头时无法在 Imageview 中设置图像,而在使用前置摄像头时可以正常工作。请帮助我

Bitmap myBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
int height = (myBitmap.getHeight() * 512 / myBitmap.getWidth());
Bitmap scale = Bitmap.createScaledBitmap(myBitmap, 512, height, true);

// 这里mediaFile是图片的路径。 // 将比例位图显示到您的 ImageView

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageResizer {

public static Bitmap decodeSampledBitmapFromFile(String filename,
   int reqWidth, int reqHeight) {
     // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options
    options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filename, options);
    // Calculate inSampleSize
      options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          return BitmapFactory.decodeFile(filename, options);
         }

        public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
     // BEGIN_INCLUDE (calculate_sample_size)
    // Raw height and width of image
  final int height = options.outHeight;
     final int width = options.outWidth;
   int inSampleSize = 1;

   if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
        && (halfWidth / inSampleSize) > reqWidth) {
    inSampleSize *= 2;
}

// This offers some additional logic in case the image has a strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might still
// end up being too large to fit comfortably in memory, so we should
// be more aggressive with sample down the image (=larger inSampleSize).

long totalPixels = width * height / inSampleSize;

// Anything more than 2x the requested pixels we'll sample down further
final long totalReqPixelsCap = reqWidth * reqHeight * 2;

while (totalPixels > totalReqPixelsCap) {
    inSampleSize *= 2;
    totalPixels /= 2;
     }
       }
 return inSampleSize;
       // END_INCLUDE (calculate_sample_size)
        }

        }

方法的使用

   Bitmap bmp = ImageResizer.decodeSampledBitmapFromFile(new  File(filePath).getAbsolutePath(), 512, 342); 

这将调整您的位图大小,以便您可以摆脱 error.process 这些在 UI 线程中的 OOM,这看起来更好。