Payday Loan Payday Loan

Hi, for today I choose to show you how to get and display all the images from SD card.

For this we’ll create an activity SDImagesGallery (the gallery), and an adapter GalleryAdapter (the adapter for the gallery).

1. Let’s begin with the xml layout for the SDImageGallery activity:

gallery_layout.xml

 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <ImageView android:id="@+id/image_view_gallery" 
        android:layout_width="match_parent" 
	android:layout_height="match_parent"
	android:contentDescription="@string/gallery"/>
 
    <Gallery 
	android:id="@+id/gallery" 
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:layout_weight="0"/>
 
</LinearLayout>

2. Create SDImageGallery activity as follows:
(Important: In order not to block the UI thread we’ll use an AsyncTask class to get the images)

 
import java.util.ArrayList;
 
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
 
 
public class GalleryActivity extends Activity {
 
	private Gallery mGallery;
	private ImageView mImageView;
	private ProgressDialog mProgressDialog;
	private ArrayList<String> mImages = new ArrayList<String>();
	private int currentIndex;
	private GetImagesAsync getImagesAsync; 
 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery_layout);
 
		mGallery = (Gallery) findViewById(R.id.gallery);
		mImageView = (ImageView) findViewById(R.id.image_view_gallery);
 
		GalleryAdapter adapter = new GalleryAdapter(this);
		mGallery.setAdapter(adapter);
 
		mImageView.setImageResource(0);
 
		setViews();	
 
		getImagesAsync = new GetImagesAsync();
		getImagesAsync.execute((Void) null);
	}
 
	public void setView() {
		mGallery.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				mImageView.setImageURI(Uri.parse(mImages.get(arg2)));
			}
		});
 
		mGallery.setCallbackDuringFling(false);		
		mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
 
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				mImageView.setImageURI(Uri.parse(mImages.get(arg2)));
			}
 
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
 
		});
	}
 
	public class GetImagesAsync extends AsyncTask<Void, Void, Boolean> {
		@Override
		protected void onPreExecute() {
			mProgressDialog = ProgressDialog.show(ImageAlbums.this,
					null, "Loading images...", true, false);
		}
 
		@Override 
		protected Boolean doInBackground(Void...voids) {
			result = getImages();
 
			return result;
		}
 
		@Override
		protected void onPostExecute(Boolean result) {
			mProgressDialog.dismiss();
			if(result) {
				adapter.setData(mImages);
				adapter.notifyDataSetChanged();	
			}else {
				Toast.makeText(SDImageGallery.this, "No SD card available", Toast.LENGTH_LONG).show();
			}
		}
	}
 
	private boolean getImages() {
 
		if(!isSdAvailable()){
			return false;
		}
 
		String[] projection = {MediaStore.Images.Media.DATA};
		String ord = MediaStore.Images.Media.DATA;
 
		ContentResolver cr = getContentResolver();
		Cursor mCursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
  				projection, 
  				null, 
  				null, 
  				ord + " ASC");	
 
  		if (mCursor != null && mCursor.moveToFirst()) 
  		{
			while(!mCursor.isAfterLast())
			{
	        		String data = mCursor.getString(mCursor.getColumnIndexOrThrow(ord));
	        		mImages.add(data);
	        		mCursor.moveToNext();
			}
			mCursor.close();
  		}
 
  		return true;
 
	}
 
	private boolean isSdAvailable()
	{
		boolean state = android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		return state;
	}
 
}

3. Create an adapter for the gallery:

 
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
 
import java.util.ArrayList;
 
public class GalleryAdapter extends BaseAdapter{
 
	Context mContext;
	int mGalleryItemBackground;
	ArrayList<String> mImages = new ArrayList<String>();
	Resources resources;
 
	public GalleryAdapter(Context context) {
		mContext = context;
		TypedArray a = mContext.obtainStyledAttributes(R.styleable.Gallery1);
        	mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
        	a.recycle();
        	resources = context.getResources();
	}
 
	@Override
	public int getCount() {
		return mImages.size();
	}
 
	@Override
	public String getItem(int index) {
		return mImages.get(index);
	}
 
	@Override
	public long getItemId(int id) {
		return id;
	}
 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView i = new ImageView(mContext);
 
		i.setImageURI(Uri.parse(mImages.get(position)));
		i.setLayoutParams(new Gallery.LayoutParams(resources.getDimension(R.dimen.mThumbnailWidth), resources.getDimension(R.dimen.mThumbnailHeight)));
		i.setBackgroundResource(mGalleryItemBackground);
 
    		return i;
	}
 
	public void setData(ArrayList<String> images) {
		mImages.addAll(images);
	}
 
	public void clearData() {
		mImages.clear();
	}
}

R.dimen.mThumbnailWidth and R.dimen.mThumbnailHeight can be defined in res/values/dimens.xml as follows:

 
<resources>
 
    <dimen name="mThumbnailWidth">120dp</dimen>
    <dimen name="mThumbnailHeight">68dp</dimen>
 
</resources>

Happy coding,
Bogdan

||||| 1 I Like It! |||||

Search

Popular

Sponsors