Deepak Rattan
Deepak Rattan

Reputation: 1299

Display image in full screen on clicking it

I am working on an instant chat application .I have 3 tabs in an Activity namely Chat,Group and Contact.In Group Tab ,i have a list view which is populated using BaseAdapter.Now list view can contain images .On clicking image ,i want to display it in full screen.

Inside adpater i am using following code on clicking imageview :

//On clicking image,display the image in full screen


 imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, FullImageActivity.class);
                intent.putExtra("image", image);
                context.startActivity(intent);              
            }
        });

Here "image" is the base 64 representation of an image .

FullImageActivity.java

    public class FullImageActivity extends AppCompatActivity {
    ImageView imgFullImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_image);

        ///findViewBYID
        imgFullImage = (ImageView) findViewById(R.id.fullImage);

        Bundle bundle = getIntent().getExtras();
        String image = bundle.getString("image");
        Bitmap bitmap = decodeImage(image);
        imgFullImage.setImageBitmap(bitmap);
    }    

    private Bitmap decodeImage(String data) {
        byte[] b = Base64.decode(data, Base64.DEFAULT);
        Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
        return bmp;
    }
}

But it is not working for me .When i click on it moves to FullIMageActivity and instantly move back to Group Tab.Please help me to fix it.

Upvotes: 3

Views: 14155

Answers (3)

Nikhil Borad
Nikhil Borad

Reputation: 2085

Simply for you best fit solution is ImageViewPopUpHelper. https://github.com/juliomarcos/ImageViewPopUpHelper

get source from this url and make a Class name "ImageViewPopUpHelper". after that whenever you want to see pop up on click, define setOnClicklistner on that image like

profile_image.setOnClickListener(new View.OnClickListener() 
{ 
   @Override public void onClick(View v) { 
       ImageViewPopUpHelper.enablePopUpOnClick(activity, profile_image, profile_image.getDrawable());
   }
});

Upvotes: 1

Ravi Vaghela
Ravi Vaghela

Reputation: 3420

Here Base64 image is big string of data that can be intent with pass to other activity is bad idea, because that in you can loss data or application get more load. that for use to make one model class that in store Base64 string and retrieve it. How? see below code.

ModelBase64.java

class ModelBase64{
   public static String base64Image;
}

now assign image string to base64Image

imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ModelBase64.base64Image=image;
            Intent intent = new Intent(context, FullImageActivity.class);
            context.startActivity(intent);              
        }
    });

Now retrive in another class

FullImageActivity.java

public class FullImageActivity extends AppCompatActivity {
ImageView imgFullImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_image);

    ///findViewBYID
    imgFullImage = (ImageView) findViewById(R.id.fullImage);

   // Bundle bundle = getIntent().getExtras();
    //String image = bundle.getString("image");
    String image = ModelBase64.base64Image;
    Bitmap bitmap = decodeImage(image);
    imgFullImage.setImageBitmap(bitmap);
}    

private Bitmap decodeImage(String data) {
    byte[] b = Base64.decode(data, Base64.DEFAULT);
    Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
    return bmp;
}
}

Upvotes: 1

Atiq
Atiq

Reputation: 14825

You are doing it wrong, this is how you can do it

In your first Activity

Convert ImageView to Bitmap First

imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

and in second Activity

 Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

Then display bitmap in your ImageView.

Upvotes: 1

Related Questions