简体   繁体   English

如何将SD卡图像添加到Coverflow?

[英]How can i add sdcard images to coverflow?

在此处输入图片说明 Here is my coverflow with drawables :( This is my Image Adapter Code 这是我与drawables的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;
}

} }

You see,5 drawable listed above.I wanna load 5 last added images from folder.How can i add sdcard images to that code. 您会看到上面列出了5个drawable。我想从文件夹中加载5个最后添加的图像。如何将sdcard图像添加到该代码中。

I'm trying to showing 5 last taken photos with coverflow. 我正在尝试显示5张最近用Coverflow拍摄的照片。 I hope somebody can help me. 我希望有人能帮助我。

EDIT(last code): 编辑(最后一个代码):

    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;
    }
}

EDIT 2 : 编辑2:

it starts and then shows 3 items from beginning .when i try look 4+ item ,it stops. 它开始,然后从开始显示3个项目。当我尝试查看4个以上项目时,它停止。 this is code -- 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; 这是代码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; 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 is final Bitmap bitmap = BitmapFactory.decodeFile(f.get(position)); --line是final Bitmap bitmap = BitmapFactory.decodeFile(f.get(position));

private String[] mFileStrings;
 ArrayList<String> f = new ArrayList<String>();

public void getFromSdcard()
{
    File file=  new File(android.os.Environment.getExternalStorageDirectory(),"Your Sdcard");

        if (file.isDirectory())
        {
            listFile = file.listFiles();//get list of files
            for (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. 您可以在sdcard中的文件夹下获取文件的路径。 But make sure the sdcard folder does not have other file formats. 但是,请确保sdcard文件夹没有其他文件格式。 Then pass the arraylist to your adapter to display the same in coverflow 然后将arraylist传递给适配器以在Coverflow中显示相同的列表

To filter files that are .png you can use the below 要过滤.png文件,您可以使用以下内容

 File dir= new File(android.os.Environment.getExternalStorageDirectory());

Then call 然后打电话

walkdir(dir);

ArrayList<String> filepath= new ArrayList<String>();//contains list of all files ending with 

public void walkdir(File dir) {
String Patternpng = ".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 根据评论,我认为您需要显示sdcard文件夹中的最后5个项目

         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));  
         }

Your adapter 您的适配器

 public class MyAdapter extends AbstractCoverFlowImageAdapter {


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return f.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

   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);
          Bitmap b = BitmapFactory.decodeFile(f.get(position));
          image.setImageBitmap(b);     
    }

  }

UPDATE: 更新:

  1. Add only last 5 file paths to your arraylist f in getFromSdcard() 在getFromSdcard()中仅将最后5个文件路径添加到您的arraylist f

  2. Your listview item count is f.size() 您的listview项计数为f.size()

  3. To get the paths you can use f.get(position) in getview(). 要获取路径,可以在getview()中使用f.get(position)。

In getFromSdcard() 在getFromSdcard()中

        for (int i = listFile.length-5; i < listFile.length; i++)
         // add only last 5 file paths from your folder

In your adapter 在您的适配器中

@Override
 public int getCount() {
// TODO Auto-generated method stub
return f.size();
}

In getView 在getView中

        ImageView image=(ImageView)vi.findViewById(R.id.ivv);
        Bitmap b = BitmapFactory.decodeFile(f.get(position));
        image.setImageBitmap(b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM