Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imagebutton.setImageURI(uri) doesn't work

now my problem is that: This is a workaround for Onclcick an ImageButton, which tries to cache the previous image Uri. Passing null effectively resets it.

private ImageButton mImageSelect;
private static  final int GALLERY_REQUEST =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);

    mImageSelect=(ImageButton) findViewById(R.id.imageselect);
    mImageSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent= new Intent(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent, GALLERY_REQUEST);
        }
    });

}

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

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

            mImageSelect.setImageURI(selectedImageUri);
            Toast.makeText(PostActivity.this, "Image Selected", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(PostActivity.this,"Image Not Selected",Toast.LENGTH_SHORT).show();
        }
    }
}
like image 892
Bilal Malik Avatar asked Apr 25 '26 16:04

Bilal Malik


1 Answers

even if fileUri is ready, setImageUri() doesn't guarantee to set the image. so its a good practice to always use third party library when dealing with image. instead of setImageUri(), use Picasso library.

Picasso.with(MainActivity.this).load(fileUri).into(mImageView)

Add as dependancy

compile 'com.squareup.picasso:picasso:2.5.2'
like image 199
Soropromo Avatar answered Apr 28 '26 05:04

Soropromo