Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 958
Deepak Rattan Avatar asked Nov 25 '25 22:11

Deepak Rattan


2 Answers

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.

like image 90
Max Avatar answered Nov 28 '25 16:11

Max


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;
}
}
like image 28
Ravi Vaghela Avatar answered Nov 28 '25 16:11

Ravi Vaghela