select image from gridview and display it full screen - android

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);
}
}
}

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);
}}

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.

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

Related

ImageView fullscreen in Activity

I have a GridView populated with images. When I click on a GridView item it should open a new Activity and show an image in full screen, but it doesn't happen.
I'm using the Glide library. I've tried to debug, but I didn't find the problem.
Any ideas?
Gallery.java
gridViewGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(AdvertisementGallery.this,FullScreenImage.class);
intent.putExtra("image",i);
startActivity(intent);
}
});
FullScreenImage.java
viewPager.clearFocus();
imageItems = Constants.items;
bitmap = getIntent().getExtras().getInt("image");
// ImagePageAdapter imagePageAdapter = new ImagePageAdapter(this,imageItems);
imagePageAdapter = new ImageAdapter(this,imageItems);
viewPager.setAdapter(imagePageAdapter);
viewPager.setCurrentItem(bitmap,false);
class ImageAdapter extends PagerAdapter {
public Context context;
public ArrayList data = new ArrayList();
public ImageAdapter(Context context, ArrayList data){
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return this.data.size();
}
#Override
public int getItemPosition(Object object) {
int position = data.indexOf(object);
return POSITION_NONE ;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View row = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.image_pager_item_list_layout, container, false);
// holder.imageTitle = (TextView) row.findViewById(R.id.text);
image = (TouchImageView) row.findViewById(R.id.imgDisplay);
row.setTag(image);
} else {
image = (ImageView) row.getTag();
}
ImageItem item = (ImageItem) data.get(position);
Glide.with(getApplicationContext())
.load(item.getImage())
.into(image);
container.addView(row);
return row;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((LinearLayout) object);
}
}
First of all, you are passing the position of the image. you need to pass the item. instead of position. you need to do following changes in the following way.
in Gallery.java
gridViewGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ImageItem item = (ImageItem) data.get(position)
Intent intent = new Intent(AdvertisementGallery.this,FullScreenImage.class);
//intent.putExtra("image",i);
intent.putExtra("imagePosition",i);
startActivity(intent);
}
});
Now, Retrieve your image in FullScreenImage.java by following way.
imageItems = Constants.items;
int position=getIntent().getExtras().getInt("imagePosition");
ImageItem item = imageItems.get(position);
bitmap = item.getImage();
To set in your ViewPager you can follow this code:
imagePageAdapter = new ImageAdapter(this,imageItems);
viewPager.setAdapter(imagePageAdapter);
viewPager.setCurrentItem(position, true);
And for the issue of
some time image display and some time is not display in viewpager.
Try to add following line .placeholder (new ColorDrawable (Color.WHITE)). so the code looks like :
Glide.with(getApplicationContext())
.load(item.getImage())
.placeholder (new ColorDrawable (Color.WHITE))
.into(image);
And, If you are getting issue only in release mode apk then try this solution
Check how to send an image from one activity to another activity via intent. In your code,what you are sending is not an image that is a position. How did you get the position in the bitmap? this is not the way. sorry for my english.
check this link
first, convert the image into bitmap then pass bitmap to other activity and receive it.

how to get image from gridview list when i click image, i'm using picasso library

i want when i click image from gridview, image send to new layout (Details Image)
this is MainActivity
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
i.putExtra("arg3", arg2);
startActivity(i);
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "okey", Toast.LENGTH_SHORT).show();
}
});
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
public static final String URL ="http://api.androidhive.info/json/movies/";
Context mContext;
int mThumbIds = 18;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(95, 95));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else {
imageView = (ImageView) convertView;
}
Picasso.with(this.mContext)
.load(URL + position +".jpg")
// .placeholder(R.drawable.loader).error(R.drawable.ic_launcher).fit()
.into(imageView);
// imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images }
in here i try get image from gridview, i try use intent, it's not work
Details Image
public class SingleViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
Intent i = getIntent();
int position = i.getExtras().getInt("arg3");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.getItemViewType(position));}}
can anyone help me
Two points:
Pass the URL of the image you selected from MainActivity to SingleViewActivity.
Use ImageLoader, and do not modify the width/heigth otherwise the memory cache will miss.
Others the cache will do everything for you.

getting image id from ImageAdapter

I'am new to android and I've a project of making a grid view of images and onclick on image it shows it , thats the ImageAdapter class code
public class ImageAdapter extends BaseAdapter{
private static LayoutInflater inflater = null;
private Activity activity;
private String mode = "";
int[] images=null ;
public ImageAdapter(Activity act, String mode , int[] images){
inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
activity = act;
this.mode = mode;
this.images= images ;
}
public ImageView getImage(int pos)
{
ImageView im = (ImageView) getItem(pos);
return im ;
}
#Override
public int getCount() {
return images.length;
}
#Override
public Object getItem(int position) {
return new Integer(images[position]);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if(mode.equalsIgnoreCase("grid")){
if (view == null) {
view = inflater.inflate(R.layout.each_image1, null);
}
ImageView iv = (ImageView)view.findViewById(R.id.imageView);
iv.setImageResource(images[position]);
} else if(mode.equalsIgnoreCase("gallery")){
if (view == null) {
view = inflater.inflate(R.layout.each_image_gallery, null);
}
ImageView iv = (ImageView)view.findViewById(R.id.imageView);
iv.setImageResource(images[position]);
}
return view;
}
}
thats my grid activity
public class GridActivity extends Activity {
GridView grid = null;
public static ImageAdapter adapter1 ;
public static ImageAdapter adapter2 ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
adapter1= new ImageAdapter(GridActivity.this, "grid" , Images.images1);
adapter2 = new ImageAdapter(GridActivity.this, "grid" , Images.images2);
Intent i = this.getIntent();
if (i!=null)
{
String unique = i.getExtras().getString("Unique");
if (unique.equals("islam"))
{
Toast.makeText(GridActivity.this, "islam", Toast.LENGTH_LONG).show();
grid = (GridView)findViewById(R.id.gridView1);
grid.setAdapter(adapter1);
}
if (unique.equals("natural"))
{
Toast.makeText(GridActivity.this, "nat", Toast.LENGTH_LONG).show();
grid = (GridView)findViewById(R.id.gridView1);
grid.setAdapter(adapter2);
}
}
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long id) {
Intent i = new Intent(GridActivity.this, imgPrevActivity.class);
i.putExtra("selectedIntex", pos);
startActivity(i);
Toast.makeText(GridActivity.this,"ddd",Toast.LENGTH_LONG).show();
}
});
}
}
and this is the activity where it supposes to show any clicked on image
public class imgPrevActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.each_image1);
ImageView im = (ImageView)findViewById(R.id.imageView);
int pos = getIntent().getExtras().getInt("selectedIntex");
// ImageAdapter adapter = new ImageAdapter(imgPrevActivity.this, "image prev", null);
long Id= GridActivity.adapter1.getItemId(pos);
im.setImageResource((int) Id);
}
}
i've tried to get the position of image clicked on from the grid
then getting the Id from the position
and setting the image view to that ID
but it doesnt work !!
thats where the images are put in arrays
public class Images {
public static int[] images1 = {
R.drawable.buds, R.drawable.cherry_34,
R.drawable.clouds_2, R.drawable.coffee_beans_2,
R.drawable.death_valley_sand_dunes
};
public static int[] images2= {
R.drawable.morning_glory_pool,
R.drawable.pink_flowers, R.drawable.sun_flower,
R.drawable.sunrise_3, R.drawable.yellow_rose_3,
};
}
You have to pass your image resource id with intent. The list item id of adapter is useless because you have new Activity on the screen.
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long id) {
Intent i = new Intent(GridActivity.this, imgPrevActivity.class);
i.putExtra("selectedIntex", grid.getAdapter().getItem(pos);
startActivity(i);
Toast.makeText(GridActivity.this,"ddd",Toast.LENGTH_LONG).show();
}
});
and change your code in your ImgPreviewActivity:
public class imgPrevActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.each_image1);
ImageView im = (ImageView)findViewById(R.id.imageView);
int pos = getIntent().getIntExtra("selectedIntex", 0);
im.setImageResource(pos);
}
}
I think it should work

setting image from grid view in dialog to another grid view in the main layout

In my main layout i have a grid view with some images from resource folder. when i click an image on the main grid view a dialog window pop ups and shows another grid view contains images from sdcard. When i click an image on the grid view on dialog i want to set the clicked image on to the position that is clicked on the main grid view.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image.add(R.drawable.split1);
image.add(R.drawable.spli21);
image.add(R.drawable.split22);
image.add(R.drawable.split31);
image.add(R.drawable.split32);
image.add(R.drawable.split33);
image.add(R.drawable.split34);
image.add(R.drawable.split41);
image.add(R.drawable.split42);
image.add(R.drawable.split43);
gv = (GridView) findViewById(R.id.gridView1);
gv.setAdapter(new AppsAdapter(MainActivity.this,image));
gv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
// TODO Auto-generated method stub
// Toast.makeText(getBaseContext(),
// "pic" + (position) + " selected",
// Toast.LENGTH_SHORT).show();
//Context context = getApplicationContext();
if(position==0)
{
createdialog(position);
}
}
});
}
public void createdialog(int pos8)
{
pos1 = pos8;
System.out.println("Position->"+pos1);
dialog = new Dialog(this);
dialog.setContentView(R.layout.content);
dialog.setTitle("Select Contents !!");
dirs.clear();
listFilesSDcard = new File(Environment.getExternalStorageDirectory()
.toString());
System.out.println("listFilesSDCard->"+listFilesSDcard);
getFolderList(listFilesSDcard, dirs);
System.out.println("ArrayList dirs Contains->"+dirs);
sgv = (GridView) dialog.findViewById(R.id.gridView);
sgv.setAdapter(new AppsAdapter1(dirs,pos1));
dialog.show();
}
/* Apps adapter for dialog gridview */
public class AppsAdapter1 extends BaseAdapter
{
ArrayList<File> dirsTemp1 = new ArrayList<File>();
//int pos5;
/*Arraylist dirs contains the sdcard image files*/
public AppsAdapter1(ArrayList<File>dirs,int pos10)
{
Log.d("In appsadapter2","Hiii");
dirsTemp1 = dirs;
pos2 = pos10;
}
//---returns the number of images---
public final int getCount()
{
return dirsTemp1.size();
}
//---returns the ID of an item---
public final Object getItem(int position)
{
return dirsTemp1.get(position);
}
public final long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
//int i=1;
View v = null;
Bitmap mOriginalBitmap = null;
//View view;
//Bitmap map1;
final File f = (File) getItem(position);
if (convertView == null)
{
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.gridicon, null);
ImageView iv = (ImageView) v.findViewById(R.id.imageView);
TextView tv = (TextView) v.findViewById(R.id.texttag);
tv.setTextColor(Color.BLACK);
String fName = f.getName();
if (f.isFile()) {
if (fName.endsWith(".png") || fName.endsWith(".jpeg")
|| fName.endsWith(".jpg")
|| fName.endsWith(".bmp"))
{
tv.setText(fName);
mOriginalBitmap =BitmapFactory.decodeFile(f.getAbsolutePath());
}
if (mOriginalBitmap != null)
{
mOriginalBitmap = Bitmap.createScaledBitmap(mOriginalBitmap, 75, 75, true);
iv.setImageBitmap(mOriginalBitmap);
}
}
}
else
{
v = convertView;
}
v.setOnLongClickListener(new OnLongClickListener()
{
public boolean onLongClick(View v)
{
// TODO Auto-generated method stub
// ***** I stuck here ****/
if (dialog != null)
dialog.dismiss();
String path = f.getAbsolutePath();
Toast.makeText(getBaseContext(),"long clicked",Toast.LENGTH_LONG).show();
Bitmap pass = BitmapFactory.decodeFile(path);
return false;
}
});
return v;
}
}
I just read images from sd card and shows it on a grid view in dialog. How did i replace the image on main grid view with the long clicked image on dialog grid view ? I got stuck with the onlong click listener of the dialog grid view .. Plz help me. thanks in advance..

Android Gallery Image Adapter onItemClickListener does'nt get called and other issues

am using the following code to trigger on clicking an image in a gallery
holder.imgPostImagesGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//handle clicks
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
System.out.println("Iamge adapter clicked");
/*Show ImageViewerfragment*/
Intent intent = new Intent(getActivity(), ImagePagerActivity.class);
//Generate Image Array first
String[] urls=new String[1];
urls[0]=feedsTableList.get(position).post_content.images.get(position).toString();
intent.putExtra(Extra.IMAGES, urls);
intent.putExtra(Extra.IMAGE_POSITION, 0);
startActivity(intent);
}
});
Image Adapter Code
public class ImageAdapter extends BaseAdapter {
public ArrayList<String> images;
private Context mContext;
public ImageAdapter(Context c,ArrayList<String> arrayImages) {
mContext = c;
images=arrayImages;
}
public int getCount() {
return images.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//http://www.edumobile.org/android/android-development/image-gallery-example-in-android/
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setLayoutParams(new Gallery.LayoutParams(320, 320));
imageView.setBackgroundResource(R.drawable.black_button);
imageLoader.displayImage(images.get(position), imageView, options);
//Add tap listeners
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return imageView;
}
}
The gallery works fine, but nothing happens on clicking/tapping an image.
And also, i seem to be having an issue with gallery scrolling, it just does'nt seem right. Scrolling lags and at times does'nt scroll at all. I am using this along with SlidingMenu library.
Update:
As mukesh pointed, i had 2 click events which i missed. But removing them also did'nt help; but adding my own clicklistener helped
//click listener for gallery
private class galleryPhotoClickListener implements OnItemClickListener{
private Context context;
public galleryPhotoClickListener(Context context){
this.context = context;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(getActivity(), ImagePagerActivity.class);
//Generate Image Array first
String[] urls=(String[]) feedsTableList.get(position).post_content.images.toArray();
urls[0]=feedsTableList.get(position).post_content.images.get(position).toString();
intent.putExtra(Extra.IMAGES, urls);
intent.putExtra(Extra.IMAGE_POSITION, 0);
startActivity(intent);
}
}
use only one 1 or 2
1. holder.imgPostImagesGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//handle clicks
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
System.out.println("Iamge adapter clicked");
/*Show ImageViewerfragment*/
Intent intent = new Intent(getActivity(), ImagePagerActivity.class);
//Generate Image Array first
String[] urls=new String[1];
urls[0]=feedsTableList.get(position).post_content.images.get(position).toString();
intent.putExtra(Extra.IMAGES, urls);
intent.putExtra(Extra.IMAGE_POSITION, 0);
startActivity(intent);
}
});
or
2. imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});

Categories

Resources