how to use a viewpager to swipe between images - android

I am working on an application where i have a gridview containing all my images from the gallery of my phone and when i click on an image it opens in another activvity (i already did this part).
I need to make the new activity swipe so i can see all the images one after the other by swiping on the screen.
When i swipe, the image changes so i see the next one etc..
I need to do this using a ViewPager .
Can anyone help me do it ?
This is my main activity code:
package com.example.gridviewgallery;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
public class MainActivityGridView extends ActionBarActivity {
int columnIndex;
Cursor cursor = null;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_grid_view);
String[] projection = {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA};
cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null,
MediaStore.Images.Thumbnails.IMAGE_ID);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
GridView gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(new GridAdapter(getApplicationContext(), cursor, columnIndex));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
cursor.moveToPosition(position);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int i = cursor.getInt(columnIndex);
Intent intent = new Intent(view.getContext(), ImageActivity.class);
intent.putExtra("id", i);
intent.putExtra("pos", position);
startActivity(intent);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my second activity code :
package com.example.gridviewgallery;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.widget.ImageView;
public class ImageActivity extends ActionBarActivity
{
MainActivityGridView magv;
int pos,id,columnIndex;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
Intent intent = getIntent();
id = intent.getExtras().getInt("id");
pos = intent.getExtras().getInt("pos");
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageURI( Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
String.valueOf(id)));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and this is my grid adapter class :
package com.example.gridviewgallery;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
public class GridAdapter extends CursorAdapter
{
//private Context mContext;
//private Cursor mCursor;
private int mColumnIndex;
//private ImageView imageView;
#SuppressWarnings("deprecation")
public GridAdapter(Context context, Cursor c, int ci) {
super(context, c);
mContext = context;
mCursor = c;
mColumnIndex = ci;
}
#Override
public void bindView(View convertView, Context context, Cursor curs)
{
ImageView imageView = (ImageView) convertView;
int id = curs.getInt(mColumnIndex);
imageView.setImageURI( Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
String.valueOf(id)));
imageView.setLayoutParams(new GridView.LayoutParams(350, 350));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
//imageView = (ImageView) convertView.findViewById(R.id.image);
}
#Override
public View newView(Context context, Cursor curs, ViewGroup parent)
{
/*imageView = new ImageView(mContext);
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.grid_image, parent, false);
/* int id = curs.getInt(mColumnIndex);
imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(id)));
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);*/
//return retView;
return new ImageView(context);
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public int mygetItemId(int position) {
return 0;
}
}
Thanks.

Related

Android GridView - notifyDataSetChanged not refreshing the GridView

I am writing an app that displays a grid of images stored on a local database using GridView. I implemented the deleteSelectedImage method triggered by a button which deletes a given image of the grid in the database. The deletion works, but I cannot get my GridView to refresh by itself... If I close the activity and then open it again, the changes are visible.
Here's my code :
The activity :
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
public class GalleryShowActivity extends AppCompatActivity {
private final String DEBUG_TAG = "GalleryShowActivity";
private DatabaseHandler db;
private Cursor c;
private ImageAdapter mBaseAdapter;
private TextView selectedImageText;
private GridView mGridView;
// currently selected position in the grid
private int mSelectedPos;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_show);
db = new DatabaseHandler(this);
c = db.getCursorOnAllImages(this);
mBaseAdapter = new ImageAdapter(this, c, db);
mSelectedPos = -1;
selectedImageText = (TextView) findViewById(R.id.selectedImageText);
selectedImageText.setText("Selected image : NONE");
mGridView = (GridView) findViewById(R.id.gridview);
mGridView.setAdapter(mBaseAdapter);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Log.d(DEBUG_TAG, "IMAGE CLICKED");
// click selects an image (can be changed later)
mSelectedPos = position;
selectedImageText.setText("Selected image : " + (position + 1));
}
});
}
public void deleteSelectedImage(View view) {
boolean exists = c.moveToPosition(mSelectedPos);
if (exists) {
boolean success = db.deletePointedPicture(c, this);
if (success) {
Log.d(DEBUG_TAG, "DELETE SUCCESS");
mBaseAdapter.notifyDataSetChanged();
// display a success message...
}
} else {
// display an error...
}
}
}
And here is the code for the ImageAdapter class :
package ch.epfl.sweng.project;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
class ImageAdapter extends BaseAdapter {
private Context mContext;
private Cursor mCursor;
private DatabaseHandler mHandler;
ImageAdapter(Context context, Cursor cursor, DatabaseHandler handler) {
mContext = context;
mCursor = cursor;
mHandler = handler;
}
#Override
public int getCount() {
return mCursor.getCount();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int rowHeight = 200;
int rowWidth = 300;
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(rowWidth, rowHeight));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Bitmap bm = mHandler.getImageByIndex(mCursor, position);
imageView.setImageBitmap(bm);
imageView.setVisibility(View.VISIBLE);
return imageView;
}
}
I also tried this :
mBaseAdapter = new ImageAdapter(this, c, db);
mGridView.setAdapter(mBaseAdapter);
Instead of :
mBaseAdapter.notifyDataSetChanged();
But it did not work.
You are implementing it wrong. Even after deletion data in cursor will be same. Data in cursor is only updated when onCreate() is again called. Either update the cursor on deleteSelectedImage method after deleting the data and then call notifyDataSetChanged or try to pass list instead of cursor in constructor and while deleting, delete data from list as well as database and then call notifyDataSetChanged.

Getting Specific Apps for gridview launcher

I am studying android launcher at the moment.
I've succeeded in making the launcher with Gridview with sample code from the web.
However, I don't want to show all the apps installed on the phone, just wanna show selected apps.
Is there any way to do that?
package com.example.gridlauncher;
import java.util.List;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
public class MainActivity extends Activity {
PackageManager myPackageManager;
public class MyBaseAdapter extends BaseAdapter {
private Context myContext;
private List<ResolveInfo> MyAppList;
MyBaseAdapter(Context c, List<ResolveInfo> l){
myContext = c;
MyAppList = l;
}
#Override
public int getCount() {
return MyAppList.size();
}
#Override
public Object getItem(int position) {
return MyAppList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
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(myContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView)convertView;
}
ResolveInfo resolveInfo = MyAppList.get(position);
imageView.setImageDrawable(resolveInfo.loadIcon(myPackageManager));
return imageView;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPackageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> intentList = getPackageManager().queryIntentActivities(intent, 0);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new MyBaseAdapter(this, intentList));
gridview.setOnItemClickListener(myOnItemClickListener);
}
OnItemClickListener myOnItemClickListener =
new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ResolveInfo cleckedResolveInfo =
(ResolveInfo)parent.getItemAtPosition(position);
ActivityInfo clickedActivityInfo =
cleckedResolveInfo.activityInfo;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName(
clickedActivityInfo.applicationInfo.packageName,
clickedActivityInfo.name);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);
}
};
}
Between
List<ResolveInfo> intentList = getPackageManager().queryIntentActivities(intent, 0);
and
gridview.setAdapter(new MyBaseAdapter(this, intentList));
just add a white list filter to remove the unneeded items from intentList.

Grid view to stay intact once imageview image is changed

I am developing a game in Android where in the alpha value is changed once the image in the imageview and the image in the grid view match. If the number of matches are equal to 4, I need to change the image in the imageview and again compare with the images in the grid view.
I change the image in the imageview using imageView.setImageResource(q);
The image gets changed but the grid view disappears. Could you please tell me what I need to do for the grid view to change intact?
I tried gridView.invalidateViews(); but in vain. I also created a new imageadapter with the same context but it still did not work. Any help would be really great!
package com.example.despicablemehunt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
final public Integer[] mThumbIds = {
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4,
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4,
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4,
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4
};
int i=0;
int p=0;
final Integer q=0;
public Context mContext;
List<Integer> imagesArrayList = new ArrayList<Integer>(Arrays.asList(mThumbIds));
Integer [] tempArrayList;
public static final Random rgenerator = new Random();
public static final Integer[] mImageIds =
{ R.drawable.icon1, R.drawable.icon2, R.drawable.icon3,R.drawable.icon4 };
#Override
protected void onCreate(Bundle savedInstanceState) {
final long time= System.currentTimeMillis();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Collections.shuffle(imagesArrayList);
tempArrayList = (Integer[])(imagesArrayList.toArray(new Integer[imagesArrayList.size()]));
final GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
final Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
final ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(q);
gridView.setOnItemClickListener(new OnItemClickListener() {
#SuppressLint("NewApi")
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(getResources().getDrawable(q).getConstantState().equals(getResources().getDrawable(tempArrayList[arg2]).getConstantState()))
{
if(p==5)
{
/*final long time1= System.currentTimeMillis();
if ((time1-time)> 20)
{
}*/
Toast.makeText(getApplicationContext(), "DONE DONE DONE", Toast.LENGTH_SHORT).show();
}
arg1.setAlpha((float) 0.4);
if((++i)== 4)
{
gridView.setAlpha((float)0.0);
p++;
final Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
ImageView imageView = (ImageView) iv;
imageView.setImageResource(q);
i=0;
gridView.invalidate();
}
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ImageAdapter extends BaseAdapter {
// Keep all Images in array
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(tempArrayList[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
return imageView;
}
}
}
To refresh the gridview you have to use gridView.invalidateViews(); So change it
after 4 correct click on gridview image why are you setting alpha 0 in gridview. So just remove the line
gridView.setAlpha((float)0.0);
//setting alpha causing the gridview invisible
so your code should be like this
if ((++i) == 4) {
p++;
final Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
ImageView imageView = (ImageView) iv;
iv.setImageResource(q);
i = 0;
gridView.invalidateViews();
}
hope this will help
Thanks

Having difficulty getting images from sdcard and displaying in gallery

I seem to have a problem loading images into my gallery from sdcard. I know I am doing something wrong with the code but don't know what. Can anyone please help me.
here is my GalleryActivity.java:
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.content.res.TypedArray;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class GalleryActivity extends Activity {
Cursor cursor;
String[] imageIDs = {MediaStore.Images.Thumbnails._ID};
GalleryActivity(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, imageIDs, null, null, MediaStore.Images.Thumbnails.IMAGE_ID);
cursor = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
{
};
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v,
int position, long id)
{
Toast.makeText(getBaseContext(),"pic" + (position + 1) + "selected", Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId (R.styleable.Gallery1_android_galleryItemBackground,0);
a.recycle();
}
public int getCount() {
return imageIDs.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageIDs));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
The error is under this bit of code which is to retrieve the images from the cdcard (Is this the correct code for retrieving images from sdcard?)
String[] imageIDs = {MediaStore.Images.Thumbnails._ID};
GalleryActivity(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, imageIDs, null, null, MediaStore.Images.Thumbnails.IMAGE_ID);
cursor = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
Hope anyone can help me?
These code lines have go into a method. You have them written outside any method.
GalleryActivity(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, imageIDs, null, null, MediaStore.Images.Thumbnails.IMAGE_ID);
cursor = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

how to view next image in imageview?

Hi I have images in grid view. when i click my grid view images, it's display in full screen. if i want view another image go back in grid view then select another image view in full screen. i feel it's hard one.. so i am trying to view images full screen view using to swipe....no idea how to create this method please guide me and give some nice codes also...
This is my working app screen shot:
source code: 1. ImageViewExample.java
public class ImageViewExample extends Activity {
/** Called when the activity is first created. */
private Cursor imagecursor, actualimagecursor;
private int image_column_index, actual_image_column_index;
GridView imagegrid;
private int count;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_image_grid();
}
private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Thumbnails._ID };
imagecursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
count = imagecursor.getCount();
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,int position, long id) {
System.gc();
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToPosition(position);
String i = actualimagecursor.getString(actual_image_column_index);
System.gc();
Intent intent = new Intent(getApplicationContext(), ViewImage.class);
intent.putExtra("filename", i);
startActivity(intent);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position,View convertView,ViewGroup parent) {
System.gc();
ImageView i = new ImageView(mContext.getApplicationContext());
if (convertView == null) {
imagecursor.moveToPosition(position);
int id = imagecursor.getInt(image_column_index);
i.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+ id));
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setLayoutParams(new GridView.LayoutParams(92, 92));
}
else {
i = (ImageView) convertView;
}
return i;
}
}
}
ViewImage.java
public class ViewImage extends Activity {
private String filename;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.gc();
Intent i = getIntent();
Bundle extras = i.getExtras();
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 2;
filename = extras.getString("filename");
ImageView iv = new ImageView(getApplicationContext());
Bitmap bm = BitmapFactory.decodeFile(filename, bfo);
iv.setImageBitmap(bm);
setContentView(iv);
}
}
After getting id of selected image, implement this..
// this Class For Grid view images
package com.thumbnailview;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class Activity_ThumbView extends Activity
{
GridView gridview;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(Activity_ThumbView.this, "" + position, 2).show();
id=parent.getPositionForView(v);
Intent i=new Intent(Activity_ThumbView.this,Activity_ImageView.class);
i.putExtra("position", position);
startActivity(i);
finish();
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context context;
public ImageAdapter(Context c )
{
context = c ;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return mThumbIds[position];
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(75, 75));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
public static Integer[] mThumbIds = {
R.drawable.a,R.drawable.icon,
R.drawable.b, R.drawable.s,
R.drawable.c, R.drawable.r,
R.drawable.d, R.drawable.q,
R.drawable.e, R.drawable.p,
R.drawable.f, R.drawable.o,
R.drawable.g, R.drawable.n,
R.drawable.h, R.drawable.m,
R.drawable.i, R.drawable.l,
R.drawable.j, R.drawable.k,
R.drawable.t,R.drawable.y,
R.drawable.u,R.drawable.x,
R.drawable.v,R.drawable.s,
R.drawable.cd,R.drawable.z,
R.drawable.bc,R.drawable.ab
};
}
// This Flip image Class
package com.thumbnailview;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class Activity_ImageView extends Activity
{
ImageView thumb_imgview;
ViewFlipper viewFlipper;
Button b_wall;
Button b_home;
// Animation a,b;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
int j;
WallpaperManager myWall;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
j=getIntent().getExtras().getInt("position");
myWall = WallpaperManager.getInstance(getApplicationContext());
b_wall=(Button) findViewById(R.id.button3);
b_home=(Button) findViewById(R.id.button1);
thumb_imgview=(ImageView) findViewById(R.id.thumb_txt_image);
thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);
gestureDetector = new GestureDetector(new MyGestureDetector());
b_wall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
myWall.setResource(Activity_ThumbView.mThumbIds[j]);
} catch (IOException e) {
e.printStackTrace();
}
}
});
b_home.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(Activity_ImageView.this,Activity_ThumbView.class);
startActivity(i);
finish();
}
});
}
public class MyGestureDetector extends SimpleOnGestureListener implements OnGestureListener
{
public boolean onFling(MotionEvent m1, MotionEvent m2, float velocityX, float velocityY)
{
try
{
if (Math.abs(m1.getY() - m2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if(m1.getX() - m2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
{
if(Activity_ThumbView.mThumbIds.length>j)
{
j++;
thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);
}
}
else if (m2.getX() - m1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
{
if(j>0)
{
j--;
thumb_imgview.setImageResource(Activity_ThumbView.mThumbIds[j]);
}
}
}
catch (Exception e)
{
}
return false;
}
}
public boolean onTouchEvent(MotionEvent event)
{
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}
}
Implement OnGestureListener in ViewImage activity and capture swipe from user.
For displaying previous or next image, use cursor.
Store all image filenames in array when you are populating into Gridview and then pass that array to another view from that array you can get all images instead of go back to gridview for selecting images..
The easiest way to do this is indeed like RajaReddy P said: create an array with the filenames/urls pass it to the ViewImage activity...
For the ViewImage activity I really would use the ViewPager! Really easy to implement and the swiping is handled for you!
More about viewpager:
http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

Categories

Resources