메인 액티비티 소스 입니다.
package com.example.sd;
import java.io.File;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener{
private Button prevBtn, nextBtn;
private ViewPager pager;
private ViewPagerAdapter adapter;
ArrayList<String> list = new ArrayList<String>();
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public Bitmap decodeSampleBitmapFromUri(String path, int reqWidth, int reqHeight){
Bitmap bm = null;
//First decode with inJustDecodeBounds = true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
//calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
//Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String targetPath = "/storage/usb0/img/";
File targetDirector = new File(targetPath);
//이미지 파일 경로 객체에 담기
File[] files = targetDirector.listFiles();
for(File file : files){
list.add(file.getAbsolutePath());
}
//페이지 이동 버튼 정의
prevBtn = (Button)findViewById(R.id.prevBtn);
prevBtn.setOnClickListener(this);
nextBtn = (Button)findViewById(R.id.nextBtn);
nextBtn.setOnClickListener(this);
//뷰페이저 정의
pager = (ViewPager)findViewById(R.id.pager);
adapter = new ViewPagerAdapter(this);
pager.setAdapter(adapter);
Toast.makeText(getApplicationContext(), "list.size():"+list.size(), 0).show();
}
@Override
public void onClick(View v) {
int viewId = v.getId();
//이전 버튼이 눌렸을 때
int cur = pager.getCurrentItem();
if(viewId == R.id.prevBtn){
if(cur > 0){
pager.setCurrentItem(cur -1);
}else{
// Toast.makeText(getApplicationContext(), cur+"", 0).show();
Toast.makeText(getApplicationContext(), "처음 페이지 입니다.", Toast.LENGTH_SHORT).show();
}
}
//다음 버튼이 눌렸을 때
else if(viewId == R.id.nextBtn){
if(cur < adapter.getCount()-1){
pager.setCurrentItem(cur +1);
}else{
Toast.makeText(getApplicationContext(), ""+adapter.getCount(), 0).show();
// Toast.makeText(getApplicationContext(), "마지막 페이지 입니다.", Toast.LENGTH_SHORT).show();
}
}
}
//뷰 페이저를 위한 어댑터
public class ViewPagerAdapter extends PagerAdapter {
private Context mContext;
public ViewPagerAdapter(Context context){
super();
mContext = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object instantiateItem(View pager, int position) {
// create a instance of the page and set data
Gpage page = new Gpage(mContext);
Bitmap bm = decodeSampleBitmapFromUri(list.get(position), 220, 220);
page.setImage(bm);
// 뷰페이저에 추가
ViewPager curPager = (ViewPager) pager;
Log.d("DBMSG", "positionCreated:"+position);
Log.d("DBMSG","paths:"+list.get(position));
curPager.addView(page, position);
return page;
}
@Override
public void destroyItem(View pager, int position, Object view) {
Log.d("DBMSG", "positionRemoved:"+position );
((ViewPager)pager).removeView((Gpage)view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
//create a instance of the page and set data
return view.equals(object);
}
}
}
. 0,1,2 까지의 뷰페이저는 정상적으로 생성이 되는데 3인덱스에 접근하는 순간 죽어버리네요..