Home Ask Login Register

Developers Planet

Your answer is one click away!

sam February 2016

select image from gridview and display it full screen

I'm using ion library (from this link) to display image and videos from phone in gridview. I just need to get the image on clicking it in gridview and display the image in another activity. Normally I would've used Integer[position] and getItem() to display the image in full screen. But how to do that here when I'm using Ion library?

public class MainActivity extends Activity {
    private MyAdapter mAdapter;
    private GridView view;

    // Adapter to populate and imageview from an url contained in the array adapter
    public class MyAdapter extends ArrayAdapter<String> {
        public MyAdapter(Context context) {
            super(context, 0);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // see if we need to load more to get 40, otherwise populate the adapter
            if (position > getCount() - 4)
                loadMore();

            if (convertView == null)
                convertView = getLayoutInflater().inflate(R.layout.image, null);

            // find the image view
            final ImageView iv = (ImageView) convertView.findViewById(R.id.image);

            // select the image view
            Ion.with(iv)
            .centerCrop()
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.error)
            .load(getItem(position));

            return convertView;
        }
    }

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

        Ion.getDefault(this).configure().setLogging("ion-sample", Log.DEBUG);

        setContentView(R.layout.activity_main);

        int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi * 2;
        view = (        

Answers


Mert Kılıç February 2016

In your adapter in the getView() method set your convert view's tag the image url. After that in

 view.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
             Intent intent = new Intent(thisclass, toClass);
             intent.putExtra(view.getTag()); // pass the url to other activity
             startActivity(intent);

        }  

    });

In the other activity get url from string extra and load the image from url.


Sohail Zahid February 2016

      view.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
                 // HERE I WANT TO GIVE THE INTENT TO DISPLAY IMAGE IN FULL SCREEN

            Intent i = new Intent(MainActivity.this, FullScreenViewActivity.class);
            i.putExtra("fullimagepath", mAdapter.get(position));
            MainActivity.this.startActivity(i);

            }  

FullScreenViewActivity

     public class FullScreenViewActivity extends Activity {
               ImageView fullImage;

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

                        Ion.getDefault(this).configure().setLogging("ion-sample", Log.DEBUG);

                        setContentView(R.layout.activity_main);

                       String s = getIntent().getStringExtra("fullimagepath");
                       fullImage = (ImageView) findViewById(R.id.fullimage);

                 Ion.with(fullImage)
                .centerCrop()
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.error)
                .load(s);
}}


David Rauca February 2016

  1. Your activity should implements AdapterView.OnItemClickListener.

  2. Add item click listener on the grid view

    view.setOnItemClickListener(this);
    
  3. Get selected image from grid view and open it on full screen on another activity:

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String imageUrl = (String) parent.getItemAtPosition(position);
        Intent intent = new Intent(MainActivity.this, FullScreenImageActivity.class);
        intent.putExtra("IMAGE_URL", imageUrl); 
        startActivity(intent);
    }
    
  4. In FullScreenImageActivity:

    String imageUrl = getIntent().getStringExtra("IMAGE_URL");
    // Load image 
    

Post Status

Asked in February 2016
Viewed 2,358 times
Voted 5
Answered 3 times

Search




Leave an answer


Quote of the day: live life