Select image from gallery and show in imageview

别等时光非礼了梦想. 提交于 2019-11-28 14:44:56

ImagePicker : please credit the developer when using their library

In your if statement inside of onActivityResult change requestCode to requestCode & 0xffff.

What many people don't notice here is that the requestCode that is coming back is coming back as a hex number not as a digit number.

In your AndroidManifest.xml add these permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Use this method to pick up images:

protected  void onImageViewClick(){
  //  ImageView imageView=(ImageView)findViewById(R.id.imageView2);
    TextView tw=(TextView)findViewById(R.id.addimage);
    tw.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectImage();
        }
    });

}


private void selectImage() {
    Intent takeImageIntent = ImagePicker.getPickImageIntent(this);
    if (takeImageIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivityForResult(takeImageIntent, REQUEST_IMAGE_CAPTURE);
    }
}

Then use this to receive them:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bitmap bitmap = ImagePicker.getBitmapFromResult(this, resultCode, data);
    if (null != bitmap && resultCode == RESULT_OK) {
    //do what you want with the bitmap here

   }
}

Did you try this?

public void onActivityResult( ....) {
      If ( resultCode == RESULT_OK) {
             Uri selectedImg = data.getData();
             Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver, selectedImg);
             If(bitmap != null) {
                yourImageView.setImageBitmap(bitmap);
             }
       }
}

put this code inside (anything which you use to go to gallery)

Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

Then call this function

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();

           Bitmap img=MediaStore.Images.Media.getBitmap(getContentResolver, filePath);


     If(img!= null) {
            image.setImageBitmap(img);
    }
}

don't forget this permission to be called

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!