How Can I Add Sdcard Images To Coverflow?
Here is my coverflow with drawables :( This is my Image Adapter Code /** The Constant IMAGE_RESOURCE_IDS. */ private static final List IMAGE_RESOURCE_IDS = new
Solution 1:
private String[] mFileStrings;
ArrayList<String> f = new ArrayList<String>();
publicvoidgetFromSdcard()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"Your Sdcard");
if (file.isDirectory())
{
listFile = file.listFiles();//get list of filesfor (int i = listFile.length-5; i < listFile.length; i++)
{
//get the length decrease it 5 . loop to last
mFileStrings[i] = listFile[i].getAbsolutePath();
f.add(listFile[i].getAbsolutePath());//add path of files to array list
System.out.println("...................................."+mFileStrings[i]);
}
}
}
You can get the path of files under a folder in your sdcard. But make sure the sdcard folder does not have other file formats. Then pass the arraylist to your adapter to display the same in coverflow
To filter files that are .png you can use the below
File dir= new File(android.os.Environment.getExternalStorageDirectory());
Then call
walkdir(dir);
ArrayList<String> filepath= newArrayList<String>();//contains list of all files ending with publicvoidwalkdir(File dir) {
StringPatternpng = ".png";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(Patternpng)){
//Do what ever u want
filepath.add( listFile[i].getAbsolutePath());
}
}
}
}
}
From the comment made i assume you need to display last 5 items from your sdcard folder
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 listfor(int j=size;j<f.size();j++)
{
System.out.println("Position = "+j);
System.out.println("Path of files"+f.get(j));
}
Your adapter
publicclassMyAdapterextendsAbstractCoverFlowImageAdapter {
@OverridepublicintgetCount() {
// TODO Auto-generated method stubreturn f.size();
}
@Overridepublic Object getItem(int position) {
// TODO Auto-generated method stubreturn position;
}
@OverridepubliclonggetItemId(int position) {
// TODO Auto-generated method stubreturn0;
}
public View getView(int position, View convertView, ViewGroup parent) {
//inflate layout//do something//use the edit 2 to get last 5 items in the arraylist.
ImageView image=(ImageView)vi.findViewById(R.id.ivv);
Bitmapb= BitmapFactory.decodeFile(f.get(position));
image.setImageBitmap(b);
}
}
UPDATE:
Add only last 5 file paths to your arraylist f in getFromSdcard()
Your listview item count is f.size()
To get the paths you can use f.get(position) in getview().
In getFromSdcard()
for (int i = listFile.length-5; i < listFile.length; i++)
//addonlylast5 file paths from your folder
In your adapter
@Override
publicintgetCount() {
// TODO Auto-generated method stubreturn f.size();
}
In getView
ImageView image=(ImageView)vi.findViewById(R.id.ivv);
Bitmap b = BitmapFactory.decodeFile(f.get(position));
image.setImageBitmap(b);
Post a Comment for "How Can I Add Sdcard Images To Coverflow?"