【问题标题】:how to set image in imageview from galary and camera? [duplicate]如何从图库和相机的 imageview 中设置图像? [复制]
【发布时间】:2015-10-19 23:47:07
【问题描述】:

我是 android 新手,我不知道如何在 galary 和相机的图像中设置 imageview。请帮助我..当我从相机中捕获图像时,该图像未在 imageview 中设置,请参见下面的代码

 private static final int CAMERA_REQUEST = 1888;
 ImageView imageView;
 public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_camera_capture);

     imageView = (ImageView) this.findViewById(R.id.imageView1);
     Button photoButton = (Button) this.findViewById(R.id.btncapture);

     photoButton.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View v) {
          Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
          startActivityForResult(cameraIntent, CAMERA_REQUEST);
     }
    });
   }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == CAMERA_REQUEST) {
   Bitmap photo = (Bitmap) data.getExtras().get("data");
   imageView.setImageBitmap(photo);
  }

 }

【问题讨论】:

  • 你解决了吗??

标签: android camera imageview


【解决方案1】:

你会得到onActivityResult()中文件的Uri,你需要从ContentResolver中找到路径f,然后创建Bitmap。 例如-

            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            profileImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            cursor.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多