【问题标题】:After taking picture from camera, it doesn't set in the image view从相机拍照后,它不会在图像视图中设置
【发布时间】:2014-06-25 15:45:44
【问题描述】:

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

我设置了写入外部存储的权限

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

【问题讨论】:

    标签: android android-camera android-gallery


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可以使用 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;
          }
      

      【讨论】:

        猜你喜欢
        • 2014-01-22
        • 1970-01-01
        • 1970-01-01
        • 2016-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多