0

我正在将 ArrayList 中的 MediaStore.Images.Media.DATA 字符串读取到一个数组中,这样我就可以将 SD 卡中的图像而不是 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.drawables 的数组中的地址,例如“R.drawable .pic02" 在数组中。

这就是我所做的,这部分工作没有问题:我从 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];
    }
4

1 回答 1

0

由于您已经有了 URIs / 文件系统路径,您可以使用android docu 中的 java doccreateFrom*方法之一来加载它。Drawable

于 2012-09-27T08:33:27.797 回答