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

2024-05-30

enter image description here Here is my coverflow with drawables :( This is my Image Adapter Code

    /** 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 个可绘制对象。我想从文件夹中加载 5 个最后添加的图像。我如何将 sdcard 图像添加到该代码中。

我正在尝试用 coverflow 展示 5 张最后拍摄的照片。 我希望有人能帮助我。

编辑(最后一个代码):

    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 :

它开始,然后从头开始显示 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)

--行是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]);
            }
        }
}

您可以获取SD卡中某个文件夹下的文件路径。但请确保 sdcard 文件夹中没有其他文件格式。然后将 arraylist 传递给您的适配器以在 coverflow 中显示相同的内容

要过滤 .png 文件,您可以使用以下命令

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

然后打电话

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

根据评论,我假设您需要显示 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));  
         }

您的适配器

 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. 仅将最后 5 个文件路径添加到 getFromSdcard() 中的数组列表 f 中

  2. 您的列表视图项目计数为 f.size()

  3. 要获取路径,可以在 getview() 中使用 f.get(position) 。

在 getFromSdcard() 中

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

在你的适配器中

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

在获取视图中

        ImageView image=(ImageView)vi.findViewById(R.id.ivv);
        Bitmap b = BitmapFactory.decodeFile(f.get(position));
        image.setImageBitmap(b);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 SD 卡图像添加到 coverflow? 的相关文章

随机推荐

  • 文档过滤器在 Java 中不起作用?

    在超过 10 个字符的文本字段中 它必须显示错误 为此 我使用了文档过滤器 JTextField field JTextField txtFld AbstractDocument document AbstractDocument fiel
  • EF 5.0 枚举未生成

    背景我在安装了 Net 4 5 的机器上使用 VS 2010 我读到这是就地安装 覆盖了 net 4 0 版本 我的项目仍然针对 4 0 而 4 5 选项不可用 但被告知没关系 因为 4 5 是就地安装 然后 我通过 nuget 安装了 E
  • Swagger:通配符路径参数

    我有一个 API 它允许传入任意路径 例如所有这些 api tags api tags foo api tags foo bar baz 是有效路径 我尝试将其描述如下 tags tag path get parameters name t
  • 除非打开开发人员工具,否则 IE8 Javascript 无法运行?

    由于某种原因 在 IE8 中 除非我在打开开发工具的情况下重新加载页面 否则 javascript 不会运行 我关闭开发人员工具并重新加载页面 然后 javascript 停止工作 我没有收到任何错误报告 无论如何它们也没有任何用处 还有其
  • 协程中未捕获异常

    我似乎无法在协程中完成错误处理 我读了很多文章并且异常处理文档 https kotlinlang org docs reference coroutines exception handling html exception propaga
  • model.predict() 返回类而不是概率

    Hello 我是第一次使用 Keras 我训练并保存了一个模型 作为 json 文件及其权重 该模型旨在将图像分为 3 个类别 我的编译方法 model compile loss categorical crossentropy optim
  • 在 SSIS 2012 中为 Oracle CLOB 转换 DT_TEXT

    我正在使用 SSIS 2012 我有一个带有 DT TEXT 类型的输出列的脚本组件 它是来自网页的 XML 我有一个 OLE DB 目标 使用 OLE DB 的本机 OLD DB Oracle 提供程序 并且该字段的表定义为 CLOB 这
  • 如何使用 QAbstractTableModel(模型/视图)将数据设置到 QComboBox?

    我希望能够设置itemData of a combobox当使用填充时QAbstractTableModel 但是 我只能从模型返回一个字符串data method 通常 当不使用模型时 可以像这样执行 Set text and data
  • 更改成员资格、角色等的默认连接字符串

    默认情况下 我的网络应用程序似乎正在使用LocalSqlServer作为用于任何应用程序服务 例如成员资格 角色 身份验证 等 的连接字符串 有什么方法可以更改默认连接字符串应该是什么 默认值是 LocalSqlServer 似乎很随意 我
  • Spring Boot中服务接口类的用途

    我的问题是关于接口类的使用 我对 Spring 还很陌生 所以如果这过于简单 请耐心等待 首先 当您可以在 BoxService 中声明 find all 时 这里拥有 IBoxService 接口有什么意义 其次 在控制器中如何使用IBo
  • 从数字列表中生成所有唯一对,n 选择 2

    我有一个元素列表 假设是整数 我需要进行所有可能的两对比较 我的方法是 O n 2 我想知道是否有更快的方法 这是我在java中的实现 public class Pair public int x y public Pair int x i
  • 如何使用 UISlider 以及如何将滑块设置为特定值?

    我是第一次使用 UIslider 首先我想知道如果值的范围是 0 到 10 如何获取滑块位置的值 其次 我希望我的滑块设置为 5 个不同的值 如 1 2 3 4 5 slider should not set between the lab
  • Java 中 .NET 的 Lambda 表达式

    我最近 再次 从 C 迁移到 Java 但我非常怀念 lambda 表达式和 C 的 IEnumerable Foreach 之类的东西 所以我正在寻找Java中的lambda表达式库 有比这更好的图书馆吗LambdaJ http code
  • 如何更改全局 git 设置以在拉取期间进行 git 合并

    目前 我的全局设置设置为在 git pull 期间执行变基操作 我希望它默认将其更改为 git merge 如何更改此设置 TL DR git config global pull rebase false 有点细节 Git 使用配置pul
  • “config”脚本存在于系统或 Homebrew 目录之外

    运行 brew doctor 并出现一些错误 我按照此链接中的建议设法解决了路径问题 如何修改 Homebrew 的 PATH https stackoverflow com questions 10343834 homebrew want
  • python 中“重载”函数的最佳方法? [复制]

    这个问题在这里已经有答案了 我正在尝试在 python 中做这样的事情 def foo x y do something at position x y def foo pos foo pos x pos y 所以我想根据我提供的参数数量调
  • Fancybox 只能水平响应高内容吗?

    我有一个网站 其中有一些非常高的图像 我希望这些图像能够以响应方式水平响应 但不能垂直响应 如果它们符合浏览器的高度 它们就会变得太小而无法正确查看 有没有一种简单的方法可以实现我所缺少的 请参阅下面的示例 单击第二个缩略图 bswift
  • 具有通用类的自定义 Android 适配器

    我正在尝试在 Android 中创建一个通用适配器 所以我不能一遍又一遍地编写它 问题是 它正在工作 但它的回收效果不是很好 它显示了我想要的内容 但是当我滚动时 它的顺序不同 public class CustomListViewAdap
  • 将整数转换为特定格式的十六进制字符串

    我是 python 新手 有以下问题 我需要将整数转换为 6 个字节的十六进制字符串 例如 281473900746245 gt xFF xFF xBF xDE x16 x05 十六进制字符串的格式很重要 int 值的长度是可变的 格式 0
  • 如何将 SD 卡图像添加到 coverflow?

    Here is my coverflow with drawables This is my Image Adapter Code The Constant IMAGE RESOURCE IDS private static final L