将图像源从R.drawable更改为外部源,如用于CoverFlow的SD卡

时间:2012-09-25 11:42:58

标签: android external drawable sd-card coverflow

我想知道为什么我的图像没有显示在封面上。有任何想法吗?这是一个基于android gallery应用程序的封面流程。它是以R.drawable为源头拍摄的。我把它改成了从SD卡上取下图像。我不知道如何使用作者推荐的代码。

以下是关于如何从外部源代码中获取代码的原始作者示例:

            //Use this code if you want to load from resources
         ImageView i = new ImageView(mContext);
         i.setImageResource(mImageIds[position]);
         i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
         i.setScaleType(ImageView.ScaleType.MATRIX);            
         return i;

以下是我试过的方法: 首先评论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;


           public View getView(int position, View convertView, ViewGroup parent) {

        ImageView i = new ImageView(mContext.getApplicationContext());
            imagecursor.moveToPosition(position);
            int id = imagecursor.getInt(image_column_index);
            i.setImageURIUri.withAppendedPathMediaStore.Images.Media.EXTERNAL_CONTENT_URI, ""+ id));
            i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
            i.setScaleType(ImageView.ScaleType.MATRIX);         

            return i;

        //return mImages[position];
    }

1 个答案:

答案 0 :(得分:2)

你应该使用原始代码,但是这样:

i.setImageBitmap(BitmapFactory.decodeFile(PATH_TO_YOUR_IMAGE_FILE_ON_SDCARD));

您可以使用数字命名图像文件: image_1.png,image_2.png ...然后使用:

i.setImageBitmap(BitmapFactory.decodeFile("image_" + position));

或者您可以将所有图像预解码为数组...