简体   繁体   中英

How to open an Image in new activity when clicked in viewpager?

I'm using viewpager to display the slideshow of the images but when I'm clicked on any one of the image it need to opened in new activity. Can anyone help me?

I'm displaying toast message when clicked on image but my actually requirement is to open the image in new activity.

The code is below:

class MyCustomPagerAdapter extends PagerAdapter{
private Context context;
private int images[];
private LayoutInflater layoutInflater;
public int i;

MyCustomPagerAdapter(Context context, int images[]) {
    this.context = context;
    this.images = images;
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return images.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public Object instantiateItem(final ViewGroup container, final int position) {
    View itemView = layoutInflater.inflate(R.layout.full_profile, container, false);

    ImageView imageView = (ImageView) itemView.findViewById(R.id.expandedImage);
    imageView.setImageResource(images[position]);

    container.addView(itemView);

    //listening to image click
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Toast.makeText(context, "you clicked image " + (position + 1), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context, ImageOpeningActivity.class);
            intent.putExtra("MY_IMAGE", images[position]);
            context.startActivity(intent);

        }
    });

    return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

}

ImageOpeningActivity.class

class ImageOpeningActivity extends AppCompatActivity {

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



    ImageView imageView = (ImageView) findViewById(R.id.myImageView);

    if(getIntent()!=null){
        int image  = getIntent().getExtras().getInt("MY_IMAGE");
        imageView.setImageResource(image);
    }
}

}

although i changed my code i getting an error, the error is below.

 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.vmax.kalyanam/com.vmax.kalyanam.ImageOpeningActivity}: java.lang.IllegalAccessException: java.lang.Class<com.vmax.kalyanam.ImageOpeningActivity> is not accessible from java.lang.Class<android.app.Instrumentation>

public class Full_profile extends Activity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayout;
//public String title = "KANDIBANDA ROJA";


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_profile);


    ViewPager viewPager;
    int images[] = {
            R.drawable.one,
            R.drawable.two,
            R.drawable.three,
            R.drawable.one
    };

    MyCustomPagerAdapter myCustomPagerAdapter;
    viewPager = (ViewPager)findViewById(R.id.viewPager);
    myCustomPagerAdapter = new MyCustomPagerAdapter(Full_profile.this, images);
    viewPager.setAdapter(myCustomPagerAdapter);

First of all do this in your code instead of Toast. Comment out Toast and paste these lines.

Intent intent = new Intent(this, ImageOpeningActivity.class);
intent.putExtra("MY_IMAGE", images[position]);
context.startActivity(intent);

step2: Create activity with the name of ImageOpeningActivity and in its xml take a Imageview , let suppose its Id is myImageView then do following in your on create

ImageView imageView = (ImageView) findViewById(R.id.myImageView);

    if(getIntent()!=null){
        int image  = getIntent().getExtras().getInt("MY_IMAGE");
        imageView.setImageResource(image);
    }

I am supposing that your images[position] is integer array.

put intent here in "listening to image click"

      imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent = new Intent(this,PhotoViewerActivity.class);
intent.putExtra("image", imageurl);
    startActivity(intent);
            }
        });



public class PhotoViewerActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 


        String url = getIntent().getStringExtra("image");
     // you can show image from url using library like glide or picasso
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_photo_viewer;
    }

    @Override
    public void onBackPressed() {
        PhotoViewerActivity.this.finish();
    }
}

create new activity with imageview in xml layout and call this from your viewpager image onclick

Intent intent = new Intent(this, ImageDetailActivity.class);

intent.putExtra("image",images[i]);
startActivity(intent);

ImageDetailActivity.java

    public class ImageDetailActivity extends AppCompatActivity {


     Drawable imageDrawable;

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



          Intent intent =  getIntent();
              imageDrawable=   getResources().getDrawable(intent.getIntExtra("image",-1)); 
            imageView.setImageDrawable(imageDrawable);
        }   

    }

Make sure you add this ImageDetailActivity to your android manifest file

Like @Pratap said you can send image url from one activity to another. But if you wanted to pass image itself as bitmap follow this method. Bitmap implements Parcelable, so you can pass it as intent extra.

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

and retrieve it in ImageDetailActivity by calling,

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

Warning: Do not pass data more than 500Kb through Intent Refer this site

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM