Reputation: 11
I want to make Gallery apps that loads images from a folder in SD card, and when I click at one of the images, it will show the image in full screen. Right now, I able to show all the images in grid view, but I don't how to show it into full screen.
Any body can help me??
This is my full code:
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageAdapter imageAdapter;
ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFromSdcard();
GridView imagegrid = (GridView) findViewById(R.id.ImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
}
public void getFromSdcard()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"images");
if (file.isDirectory())
{
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++)
{
f.add(listFile[i].getAbsolutePath());
}
}
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return f.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(
R.layout.gallery, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
holder.imageview.setImageBitmap(myBitmap);
return convertView;
}
}
class ViewHolder {
ImageView imageview;
}
}
I just start with programming, so please give some explanations, or it will be great if you also give the full code :)
Upvotes: 1
Views: 5727
Reputation: 596
First create a drawable folder in resource where you put images
MainActivity.java
public class GalleryScreen extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
AlertDialog.Builder myAlertDialog;
Context context;
int newpos=0;
private ImageView mSwitcher;
WallpaperManager myWallpaperManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
}
public void onResume()
{
super.onResume();
context=this;
myWallpaperManager = WallpaperManager.getInstance(context);
mSwitcher = (ImageView) findViewById(R.id.imageviewid);
mSwitcher.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myAlertDialog = new AlertDialog.Builder(context);
// myAlertDialog.setTitle("--- Title ---");
myAlertDialog.setMessage("Do you want to set as wallpaper");
myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
try {
myWallpaperManager.setResource(mThumbIds[newpos]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// do something when the OK button is clicked
}});
myAlertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.cancel();
// do something when the Cancel button is clicked
}});
myAlertDialog.show();
}
});
Gallery g = (Gallery) findViewById(R.id.galleryid);
g.setAdapter(new ImageAdapter(context));
g.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
mSwitcher.setImageResource(mThumbIds[position]);
newpos=position;
}
public void onNothingSelected(AdapterView<?> parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
return i;
}
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.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 i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return i;
}
private Context mContext;
}
private static final Integer[] mThumbIds = {
R.drawable.nature, R.drawable.lilyflowerplant,
R.drawable.heartlovecomputer, R.drawable.dolphin,R.drawable.youtube,R.drawable.twitter,R.drawable.sunset,R.drawable.androidplatform};
private Integer[] mImageIds = {
R.drawable.nature};
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/header">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/logo"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<ImageView
android:id="@+id/imageviewid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<Gallery
android:id="@+id/galleryid"
android:layout_width="match_parent"
android:layout_height="60dp" .
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:spacing="16dp"/>
</RelativeLayout>
Upvotes: 1
Reputation: 3393
Well firstly I would set an onclickListener() on the grid view which would call a method to open up an activity with the desired image in full. I'm not at my computer so I can't really type any code out, but what I mentioned should put you in the right direction. Good luck!
Upvotes: 0