Created
July 30, 2014 16:47
-
-
Save gokhanaliccii/2cfc771337121ef0cc07 to your computer and use it in GitHub Desktop.
Android swipe adaptor loading images from sdcard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.provider.MediaStore; | |
import android.support.v4.view.PagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
public class SwipeAdaptor extends PagerAdapter{ | |
Context context; | |
int columnIndex; | |
public SwipeAdaptor(Context context) { | |
this.context = context; | |
} | |
private void initCursor(){ | |
String[] projection = { MediaStore.Images.Thumbnails._ID }; | |
this.cursor = getApplicationContext().getContentResolver().query( | |
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection,null,null, MediaStore.Images.Thumbnails.IMAGE_ID); | |
this.columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); | |
} | |
@Override | |
public int getCount() { | |
return cursor.getCount(); | |
} | |
@Override | |
public boolean isViewFromObject(View arg0, Object arg1) { | |
return arg0 == ((View) arg1); | |
} | |
@Override | |
public Object instantiateItem(ViewGroup container, int position) { | |
ImageView iv = new ImageView(context); | |
// imleci bulunduğu indexe ilerletiyor. | |
cursor.moveToPosition(position); | |
int imageID = cursor.getInt(columnIndex); | |
// sorgudan bulunan sonucu resime baglıyor. | |
iv.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID)); | |
((ViewPager) container).addView(iv); | |
return iv; | |
} | |
@Override | |
public void destroyItem(ViewGroup container, int position, Object object) { | |
((ViewPager) container).removeView((View) object); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment