代码之家  ›  专栏  ›  技术社区  ›  Kevik

将URI的ArrayList<String>读取到coverflow应用程序中,而不是R.drawables

  •  0
  • Kevik  · 技术社区  · 12 年前

    我正在将数组列表中的MediaStore.Images.Media.DATA字符串读取到数组中,这样我就可以使用SD卡中的图像,而不是coverflow图库应用程序中R.drawable文件夹中的图像。并撞上了砖墙。具有读取图像信息的整数类型与字符串类型的问题。

    能够将SD卡中所有图像的URI获取到数组列表中,但不知道下一步该怎么做。

    是否值得继续使用这种方法,还是最好放弃这种方法,尝试另一种方法来完成将此coverflow应用程序的图像源从R.drawables文件夹更改为SD卡的任务。

    这是我所做的

    首先,以下是它从android中的R.drawable文件夹中获取图像的方法,使用数组作为过程的一部分:

          public class ImageAdapter extends BaseAdapter {
           int mGalleryItemBackground;
           private Context mContext;
    
           private FileInputStream fis;
    
          // private Integer[] mImageIds = {
           //       R.drawable.pic01,
             //      R.drawable.pic02,
                //    R.drawable.pic03,
                //  R.drawable.pic04,
               //   R.drawable.pic05,
               //   R.drawable.pic06,
              //   R.drawable.pic07,
             //     R.drawable.pic08,
            //    R.drawable.pic09
           //   };
    
           private ImageView[] mImages;
    

    我的想法是使用光标从SD卡中读取类似“//mnt/sdcard/pic01.png”的URI,并使用这些地址,而不是数组中类似“R.drawable.pic02”的R.drawable中的数组中的地址。

    这就是我所做的,这部分工作没有问题: 我从SD卡上的图像中获得了所有的URI,并将它们存储在arrayList中,到目前为止一切都很好。我通过使用Toast在活动屏幕上显示加载的URI,验证了它的完美工作。

           ArrayList<String> list1 = new ArrayList<String>();
    
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor cur = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String pic = cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA));
                list1.add(pic);
            //    Toast.makeText(CoverFlowExample.this, "data name: " + pic, Toast.LENGTH_SHORT).show();
             //   Log.d("TAG", "ID: " + pic);
            }
           }
        cur.close();
    

    接下来,我试图将这些字符串读取到用于R.drawables数组的数组中,这就是我遇到麻烦的地方:程序使用Integer作为数组的类型。所以我猜这个数组是错误的读取URI的地方。如果这是错误的地方。现在该怎么办?

             Integer[] mImageIds = new Integer[list1.size()];
    
        for(int t = 0; t<= list1.size(); t++){
            mImageIds[t]= list1.get(t); // type mismatch cannot convert string to integer
        }
    
        Toast.makeText(CoverFlowExample.this, "data name: " + y , Toast.LENGTH_SHORT).show();
    

    对于这一部分,我得到的错误是“类型不匹配,无法将字符串转换为整数”:

          mImageIds[t]= list1.get(t);
    

    以下是应用程序getview部分的其余代码:我现在正试图弄清楚下一步该怎么做。

               public View getView(int position, View convertView, ViewGroup parent) {
    
            //Use this code if you want to load from resources from external source
            ImageView i = new ImageView(mContext);
    
                // this method is what someone suggested to use but i don't know how to use this, what do i put for "image" and how to read URIs from arrayList to this?
            i.setImageBitmap(BitmapFactory.decodeFile("image_" + position));
    
                // this works for reading in only one image from the SD card into the coverflow app
            i.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/pic04.png"));
    
       // when getting from R.drawable images -->  i.setImageResource(mImageIds[position]);
    
            i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
            i.setScaleType(ImageView.ScaleType.MATRIX);         
            return i;
    
            //return mImages[position];
        }
    
    1 回复  |  直到 12 年前
        1
  •  0
  •   Heiko Rupp    12 年前

    由于您已经有了URI/文件系统路径,因此可以使用 createFrom* 方法 Drawable java doc in android docu 加载。