【问题标题】:Taking a photo and show it in ImageView拍照并在 ImageView 中显示
【发布时间】:2014-11-04 17:32:12
【问题描述】:

我想像这样用安卓相机拍照:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

this.startActivityForResult(intent, Globals.REQUEST_CODE_CAMERA)

并将其存储在 ImageView 中:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == Globals.REQUEST_CODE_CAMERA) {
        if(resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            Bitmap bitmap = (Bitmap) bundle.get("data");

            this.imageViewPhoto.setImageBitmap(bitmap);
        }
    }
}

我的 ImageView 是这样配置的:

<ImageView
        android:id="@+id/nfcresult_imageview_photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        android:adjustViewBounds="true"
        android:clickable="true"
        android:contentDescription="@string/imageview_photo_description" />

一切正常,但 ImageView 上显示的照片比 相机拍摄的照片。我想做的是在我的 ImageView 并将 OnClickListener 添加到 ImageView 以打开一个对话框 它以原始尺寸和分辨率显示原始照片。它不能 很难做到,但我真的不知道怎么做。

要创建对话框并显示照片,我这样做:

ImageView clone = new ImageView(this);
clone.setImageBitmap(((BitmapDrawable)this.imageViewPhoto.getDrawable()).getBitmap());

DialogManager.showImageDialog(this, this.getResources().getString(R.string.title_photo), clone);

showImageDialog:

 public static void showImageDialog(Context context, String title, ImageView imageView) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    builder.setTitle(title);
    builder.setCancelable(false);
    builder.setView(imageView);
    builder.setPositiveButton(context.getResources().getString(R.string.button_back), new DialogInterface.OnClickListener() {
        /**
         * 
         */
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    builder.create().show();
}

对话框现在显示照片,其中照片的大小存储在 imageView 中 但我想以原始尺寸和原始分辨率显示原始照片 但正如我已经说过的,ImageView 应该显示原始版本的较小版本 照片。

我怎样才能做到这一点?

【问题讨论】:

标签: android android-imageview


【解决方案1】:

而不是 android:layout_width="wrap_content" 将 imageview 的宽度和高度设置为特定的 dp 值

<ImageView
        android:id="@+id/nfcresult_imageview_photo"
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:src="@drawable/ic_launcher" 
        android:adjustViewBounds="true"
        android:clickable="true"
        android:contentDescription="@string/imageview_photo_description" />

保持高度和宽度的纵横比(而不是100和200,选择正确的值)

【讨论】:

    【解决方案2】:

    您必须在您的意图中添加一个名为“MediaStore.EXTRA_OUTPUT”的额外内容,您必须在其中设置要存储图片的 uri。 调用回调函数后,您可以从选择的 uri 加载图片。 在那里你可以得到全分辨率的图片。

    【讨论】:

      【解决方案3】:

      解决方案:

      /**
       * 
       */
      private void performAddPhoto() {
          String timeStamp = Clock.getTimeStamp();
          String fileName = "Food_Shot_" + timeStamp + ".jpg";
      
          this.imagePath = Environment.getExternalStorageDirectory() + "/images/" + fileName;
      
          File file = new File(this.imagePath);
          file.getParentFile().mkdirs();
      
          try {
              file.createNewFile();
          } catch (IOException e) {
              e.printStackTrace();
          }
      
          Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      
          intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
          intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
      
          startActivityForResult(intent, Globals.REQUEST_CODE_CAMERA);
      }
      
      /**
       * 
       */
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if(requestCode == Globals.REQUEST_CODE_CAMERA) {
              if(resultCode == RESULT_OK) {
                  this.onPhotoTaken();
              }
          }
      }
      
      /**
       * 
       */
      private void onPhotoTaken() {
          this.imageTaken = true;
      
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inSampleSize = 4;
      
          Bitmap bitmap = BitmapFactory.decodeFile(this.imagePath, options);
          this.imageViewPhoto.setImageBitmap(bitmap);
      }
      
      /**
       * 
       */
      private void performShowPhoto() {
          ImageView imageView = new ImageView(this);
          Bitmap bitmap = BitmapFactory.decodeFile(this.imagePath);
      
          imageView.setImageBitmap(bitmap);
      
          DialogManager.showImageDialog(this, this.getResources().getString(R.string.title_photo), imageView);
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-08
        • 2015-10-05
        • 1970-01-01
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-01
        相关资源
        最近更新 更多