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.
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With