将从相机拍摄的图像设置为ImageView

时间:2014-01-20 16:56:52

标签: android android-intent bitmap camera imageview

我正在尝试将从相机拍摄的图像设置为ImageView。我启动了摄像头意图然后在OnActivityResult中得到了NullPointerException,我不明白错误。

在这里,我发布了相机意图,并将图像存储在手机的图库中:

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
imageUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_REQUEST);

现在,相机的图像标记保存在手机图库中(高质量)。 在onActivityResult中,我想将图像放入ImageView。这是我的代码:

else if (requestCode == CAMERA_REQUEST)
{
  if (resultCode == RESULT_OK)
   {
     imageUri = data.getData();
             try 
             {
               Bitmap bitmap = Media.getBitmap(getContentResolver(), imageUri); //NullPointerException
               myImageView.setImageBitmap(bitmap);
             } 
          catch (IOException e) 
             {
              e.printStackTrace();
             }

我有NullPointerException,为什么?我该怎么解决呢?

2 个答案:

答案 0 :(得分:1)

这会将您的结果保存在mediaStore中,因此data.getData();将返回NULL

pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

删除它,您将获得在ImageView上显示的数据。但是,如果您直接从data获取图片,则质量很差

答案 1 :(得分:0)

您的代码不完整,但您没有添加logcat错误! 所以在这种情况下很难引导你!但你想要实现的并不是那么困难。试试这个:

public class LaunchCamera extends Activity {
 ImageView imVCature_pic;
 Button btnCapture;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_launch_camera);
  initializeControls();
 }

 private void initializeControls() {
  imVCature_pic=(ImageView)findViewById(R.id.imVCature_pic);
  btnCapture=(Button)findViewById(R.id.btnCapture);
  btnCapture.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    /* create an instance of intent
     * pass action android.media.action.IMAGE_CAPTURE 
     * as argument to launch camera
     */
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    /*create instance of File with name img.jpg*/
    File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
    /*put uri as extra in intent object*/
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    /*start activity for result pass intent as argument and request code */
    startActivityForResult(intent, 1);
   }
  });

 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  //if request code is same we pass as argument in startActivityForResult
  if(requestCode==1){
   //create instance of File with same name we created before to get image from storage
   File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
   //get bitmap from path with size of
   imVCature_pic.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 600, 450));
   }
 }
 public static Bitmap decodeSampledBitmapFromFile(String path,
   int reqWidth, int reqHeight) { 
  // First decode with inJustDecodeBounds=true to check dimensions
  final BitmapFactory.Options options = new BitmapFactory.Options();
  //Query bitmap without allocating memory
  options.inJustDecodeBounds = true;
  //decode file from path
  BitmapFactory.decodeFile(path, options);
  // Calculate inSampleSize
  // Raw height and width of image
  final int height = options.outHeight;
  final int width = options.outWidth;
  //decode according to configuration or according best match
  options.inPreferredConfig = Bitmap.Config.RGB_565;
  int inSampleSize = 1;
  if (height > reqHeight) {
   inSampleSize = Math.round((float)height / (float)reqHeight);
  }
  int expectedWidth = width / inSampleSize;
  if (expectedWidth > reqWidth) {
   //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
   inSampleSize = Math.round((float)width / (float)reqWidth);
  }
  //if value is greater than 1,sub sample the original image
  options.inSampleSize = inSampleSize;
  // Decode bitmap with inSampleSize set
  options.inJustDecodeBounds = false;
  return BitmapFactory.decodeFile(path, options);
 }
}

完整的代码段描述得很清楚here