Reputation: 181
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 = (GridView) findViewById(R.id.results);
view.setNumColumns(cols);
mAdapter = new MyAdapter(this);
view.setAdapter(mAdapter);
loadMore();
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
}
});
}
Cursor mediaCursor;
public void loadMore() {
if (mediaCursor == null) {
mediaCursor = getContentResolver().query(MediaStore.Files.getContentUri("external"), null, null, null, null);
}
int loaded = 0;
while (mediaCursor.moveToNext() && loaded < 10) {
// get the media type. ion can show images for both regular images AND video.
int mediaType = mediaCursor.getInt(mediaCursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE));
if (mediaType != MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
&& mediaType != MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO) {
continue;
}
loaded++;
String uri = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
File file = new File(uri);
// turn this into a file uri if necessary/possible
if (file.exists())
mAdapter.add(file.toURI().toString());
else
mAdapter.add(uri);
}
}
}
Upvotes: 1
Views: 2591
Reputation: 1583
Your activity should implements AdapterView.OnItemClickListener.
Add item click listener on the grid view
view.setOnItemClickListener(this);
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);
}
In FullScreenImageActivity:
String imageUrl = getIntent().getStringExtra("IMAGE_URL");
// Load image
Upvotes: 0
Reputation: 8149
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);
}}
Upvotes: 3
Reputation: 81
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.
Upvotes: 0