从相机拍摄照片后,它不会在图像视图中设置

时间:2014-06-25 15:45:45

标签: android android-camera android-gallery

在我的应用中,我需要设置用户图片。 我打开一个对话框,询问是否打开图库或相机,之后,获取结果并设置在imageview中。 它适用于画廊,但从相机拍照后,它没有在图像视图中设置(logcat上没有显示) 有人能帮助我吗?

我将permision设置为写入外部存储

public void TakePictureFromGallery(){
        Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, ResultIntentLoad);
    }

    public void TakePictureFromCamera(){
        Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePicture, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
        switch(requestCode) {
        case 0:
            if(resultCode == RESULT_OK){ 
                Uri selectedImage = imageReturnedIntent.getData();
                TakePicture.setImageURI(selectedImage);   //Non so perchè non mi inserisce poi la foto

                Confirm.setTextColor(Color.parseColor("#000000"));
                   Confirm.setEnabled(true);
            }

        break; 
        case 1:
            if(resultCode == RESULT_OK){  
                Uri selectedImage = imageReturnedIntent.getData();
                TakePicture.setImageURI(selectedImage);

                Confirm.setTextColor(Color.parseColor("#000000"));
                   Confirm.setEnabled(true);
            }
        break;
        }

        }

    public void ShowDialog(){
        //Mostro una dialog dove l'utente può scegliere se aprire la galleria o la fotocamera
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("Security and identification");
        builder.setMessage("Some shops can ask you to show an ID when paying with Satispay. We suggest to to choose a an easily identificable photo of you.");
        builder.setCancelable(false);
        builder.setNeutralButton("Lybrary",new android.content.DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                TakePictureFromGallery();  //Prendo la foto dalla gallery
                dialog.dismiss();
                }
         });
        builder.setPositiveButton("Take",new android.content.DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int id){
                TakePictureFromCamera(); //Avvio l'intent per la fotocamera 
                dialog.dismiss();
                }
          });

        builder.show();
    }

2 个答案:

答案 0 :(得分:0)

intent.getData()会返回您使用的缩略图的位图。但是,对于全尺寸图片,您需要先保存图像,然后才能在ImageView中显示图像。在这里查看android文档:http://developer.android.com/training/camera/photobasics.html#TaskPath

答案 1 :(得分:0)

您可以使用 decodeUri 函数将uri转换为位图。此函数将重新调整位图。你可以设置它,

BitmapImg.setImageBitmap(decodeUri(ImageUri));

这是 decodeUri 函数,

private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {

         Bitmap resizedBitmap = null;
         errSmallImage = false;

         BitmapFactory.Options o = new BitmapFactory.Options();
         o.inJustDecodeBounds = true;
         BitmapFactory.decodeStream( getContentResolver().openInputStream(selectedImage), null, o);

         final int REQUIRED_SIZE = 100;
         int width_tmp = o.outWidth, height_tmp = o.outHeight;

         if(width_tmp >=300 || height_tmp >=300 ){

             System.out.println("decodeUri : Original Resolution : , "+width_tmp+"x"+height_tmp);

             int scale = 1;
             while (true) {
                 if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                     break;
                 }
                 width_tmp /= 2;
                 height_tmp /= 2;
                 scale *= 2;
             }


             BitmapFactory.Options o2 = new BitmapFactory.Options();
             o2.inSampleSize = scale;
             //return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
             Bitmap b = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
             Matrix matrix = new Matrix();
             float rotation = rotationForImage(context, selectedImage);
             if (rotation != 0f) {
                   matrix.preRotate(rotation);
              }
             resizedBitmap = Bitmap.createBitmap(b, 0, 0, width_tmp, height_tmp, matrix, true);
         }else{
             errSmallImage=true;
             resizedBitmap = null;
         }
         return resizedBitmap;
     }
        public static float rotationForImage(Context context, Uri uri) {
            if (uri.getScheme().equals("content")) {
            String[] projection = { Images.ImageColumns.ORIENTATION };
            Cursor c = context.getContentResolver().query(
                    uri, projection, null, null, null);
            if (c.moveToFirst()) {
                return c.getInt(0);
            }
        } else if (uri.getScheme().equals("file")) {
            try {
                ExifInterface exif = new ExifInterface(uri.getPath());
                int rotation = (int)exifOrientationToDegrees(
                        exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_NORMAL));
                return rotation;
            } catch (IOException e) {
                Log.e("Photo Import", "Error checking exif", e);
            }
        }
            return 0f;
        }

        private static float exifOrientationToDegrees(int exifOrientation) {
        if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
            return 90;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
            return 180;
        } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
            return 270;
        }
        return 0;
    }