這裏是我的CoverFlow與可繪:( 這是我的形象適配器代碼如何添加SD卡圖像到coverflow?
/** The Constant IMAGE_RESOURCE_IDS. */
private static final List<Integer> IMAGE_RESOURCE_IDS = new ArrayList<Integer>(DEFAULT_LIST_SIZE);
/** The Constant DEFAULT_RESOURCE_LIST. */
private static final int[] DEFAULT_RESOURCE_LIST = {
R.drawable.promo_blue_bg_medium,
R.drawable.promo_green_bg_medium,
R.drawable.flow,
R.drawable.promo_yellow_bg_medium,
R.drawable.promo_black_bg_medium ,
};
/** The bitmap map. */
private final Map<Integer, WeakReference<Bitmap>> bitmapMap = new HashMap<Integer, WeakReference<Bitmap>>();
private final Context context;
/**
* Creates the adapter with default set of resource images.
*
* @param context
* context
*/
public ResourceImageAdapter(final Context context) {
super();
this.context = context;
setResources(DEFAULT_RESOURCE_LIST);
}
/**
* Replaces resources with those specified.
*
* @param resourceIds
* array of ids of resources.
*/
public final synchronized void setResources(final int[] resourceIds) {
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/CamWay/";
File targetDirector = new File(targetPath);
IMAGE_RESOURCE_IDS.clear();
for (final int resourceId : resourceIds) {
IMAGE_RESOURCE_IDS.add(resourceId);
}
notifyDataSetChanged();
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getCount()
*/
@Override
public synchronized int getCount() {
return IMAGE_RESOURCE_IDS.size();
}
/*
* (non-Javadoc)
*
* @see pl.polidea.coverflow.AbstractCoverFlowImageAdapter#createBitmap(int)
*/
@Override
protected Bitmap createBitmap(final int position) {
Log.v(TAG, "creating item " + position);
final Bitmap bitmap = ((BitmapDrawable) context.getResources().getDrawable(IMAGE_RESOURCE_IDS.get(position)))
.getBitmap();
bitmapMap.put(position, new WeakReference<Bitmap>(bitmap));
return bitmap;
}
}
你看,5繪製上市above.I想負荷5最後添加圖片來自folder.How我可以SD卡圖像添加到該代碼。
我試圖用顯示5張CoverFlow的最後拍攝的照片。 我希望有人能幫助我。
EDIT(最後一個代碼):
public class ResourceImageAdapter extends AbstractCoverFlowImageAdapter {
//Dosya alımı başlangıç
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
public ImageAdapter(Context c) {
mContext = c;
}
void add(String path){
itemList.add(path);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@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(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220, 220);
imageView.setImageBitmap(bm);
return imageView;
}
public Bitmap decodeSampledBitmapFromUri(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;
}
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;
}
}
ImageAdapter myImageAdapter;
//Burası Dosya alımı bitimi
/** The Constant TAG. */
private static final String TAG = ResourceImageAdapter.class.getSimpleName();
/** The Constant DEFAULT_LIST_SIZE. */
private static final int DEFAULT_LIST_SIZE = 20;
/** The Constant IMAGE_RESOURCE_IDS. */
private static final List<Integer> IMAGE_RESOURCE_IDS = new ArrayList<Integer>(DEFAULT_LIST_SIZE);
/** The Constant DEFAULT_RESOURCE_LIST. */
private static final int[] DEFAULT_RESOURCE_LIST = {
R.drawable.promo_blue_bg_medium,
R.drawable.promo_green_bg_medium,
R.drawable.flow,
R.drawable.promo_yellow_bg_medium,
R.drawable.promo_black_bg_medium ,
};
private String[] mFileStrings;
ArrayList<String> f = new ArrayList<String>();
public void getFromSdcard()
{
File file= new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() ,"CamWay");
if (file.isDirectory())
{
File[] listFile = file.listFiles();//get list of filess
mFileStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++)
{
mFileStrings[i] = listFile[i].getAbsolutePath();
f.add(listFile[i].getAbsolutePath());//add path of files to array list
System.out.println("...................................."+mFileStrings[i]);
}
}
}
/** The bitmap map. */
private final Map<Integer, WeakReference<Bitmap>> bitmapMap = new HashMap<Integer, WeakReference<Bitmap>>();
private final Context context;
/**
* Creates the adapter with default set of resource images.
*
* @param context
* context
*/
public ResourceImageAdapter(final Context context) {
super();
this.context = context;
setResources(DEFAULT_RESOURCE_LIST);
}
/**
* Replaces resources with those specified.
*
* @param resourceIds
* array of ids of resources.
*/
public final synchronized void setResources(final int[] resourceIds) {
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/CamWay/";
File targetDirector = new File(targetPath);
IMAGE_RESOURCE_IDS.clear();
for (final int resourceId : resourceIds) {
IMAGE_RESOURCE_IDS.add(resourceId);
}
notifyDataSetChanged();
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getCount()
*/
@Override
public synchronized int getCount() {
return IMAGE_RESOURCE_IDS.size();
}
/*
* (non-Javadoc)
*
* @see pl.polidea.coverflow.AbstractCoverFlowImageAdapter#createBitmap(int)
*/
@Override
protected Bitmap createBitmap(final int position) {
Log.v(TAG, "creating item " + position);
final Bitmap bitmap = BitmapFactory.decodeFile(f.get(position));
bitmapMap.put(position, new WeakReference<Bitmap>(bitmap));
return bitmap;
}
}
編輯2:
啓動,然後顯示從開始3項。當我嘗試一下4+項目,它停止。 這是代碼 - getFromSdcard() ; int size= f.size()-5; //get the size of arraylist then decrease it by 5 //then loop from that point to your arraylist size //to get the last 5 items in the list for(int j=size;j<f.size();j++) { System.out.println("Position = "+j); System.out.println("Path of files"+f.get(j)); } final Bitmap bitmap = BitmapFactory.decodeFile(f.get(position)); bitmapMap.put(position, new WeakReference<Bitmap>(bitmap)); return bitmap;
04-06 21:41:05.013: E/AndroidRuntime(11217): at com.project.smyrna.camway.ResourceImageAdapter.createBitmap(ResourceImageAdapter.java:152)
--line是final Bitmap bitmap = BitmapFactory.decodeFile(f.get(position));
太感謝你了。我明白,但我無法實現。最終位圖bitmap =((BitmapDrawable)context.getResources()。getDrawable(f.get(position))) - 它說f應該是int。我有點困惑 – 2013-04-06 17:20:26
位圖b = BitmapFactory.decodeFile(f.get (位置));檢查編輯 – Raghunandan 2013-04-06 17:24:16
04-06 20:29:24.075:E/AndroidRuntime(28472):引起:java.lang.IndexOutOfBoundsException:索引2無效,大小爲0 - 最終位圖位圖= BitmapFactory.decodeFile(f.get (position)); ----我得到了這個異常。我想錯了,什麼大小?!??我想了解--- – 2013-04-06 17:32:08