Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-GridView中资产文件夹中的图像_Android_Image_Android Gridview - Fatal编程技术网

Android-GridView中资产文件夹中的图像

Android-GridView中资产文件夹中的图像,android,image,android-gridview,Android,Image,Android Gridview,我一直致力于创建图像的网格视图,图像显示在资产文件夹中。link帮助我使用位图读取它。我目前拥有的代码是: public View getView(final int position, View convertView, ViewGroup parent) { try { AssetManager am = mContext.getAssets(); String list[] = am.list(""); int count_files =

我一直致力于创建图像的网格视图,图像显示在资产文件夹中。link帮助我使用位图读取它。我目前拥有的代码是:

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

  try 
    {
     AssetManager am = mContext.getAssets();
     String list[] = am.list("");
     int count_files = imagelist.length;
     for(int i= 0;i<=count_files; i++)
     {
      BufferedInputStream buf = new BufferedInputStream(am.open(list[i]));
      Bitmap bitmap = BitmapFactory.decodeStream(buf);
      imageView.setImageBitmap(bitmap);
      buf.close();
     }
   }   
   catch (IOException e) 
   {
   e.printStackTrace();
   }
  }
非常感谢您事先提供的帮助,
Saran

无需每次都阅读所有项目。只读getView方法调用中给定位置的项。此时只显示该项目

BufferedInputStream buf = new BufferedInputStream(am.open(list[position]));

Saran,下面是我用来在画廊的资产文件夹中显示图像的内容。我想gridview也是这样:

public class myActivitye extends Activity
{
    private Gallery mGallery;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mGallery = (Gallery) findViewById(R.id.mygalleryinxml);

        //load images into memory
        mBitArray = new Bitmap[4];
        try
        {
            //these images are stored in the root of "assets"
            mBitArray[0] = getBitmapFromAsset("pic1.png");
            mBitArray[1] = getBitmapFromAsset("pic2.png");
            mBitArray[2] = getBitmapFromAsset("pic3.png");
            mBitArray[3] = getBitmapFromAsset("pic4.png");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        mGallery.setAdapter(new GalleryAdapter(this, mBitArray));
    }

    public class GalleryAdapter extends BaseAdapter
    {
        //member variables
        private Context mContext;
        private Bitmap[] mImageArray;

        //constructor
        public GalleryAdapter(Context context, Bitmap[] imgArray)
        {
            mContext = context;
            mImageArray = imgArray;
        }

        public int getCount()
        {
            return mImageArray.length;
        }

        public Object getItem(int position)
        {
            return position;
        }

        public long getItemId(int position)
        {
            return position;
        }

        //returns the individual images to the widget as it requires them
        public View getView(int position, View convertView, ViewGroup parent)
        {
            final ImageView imgView = new ImageView(mContext);

            imgView.setImageBitmap(mImageArray[position]);

            //put black borders around the image
            final RelativeLayout borderImg = new RelativeLayout(mContext);
            borderImg.setPadding(20, 20, 20, 20);
            borderImg.setBackgroundColor(0xff000000);//black
            borderImg.addView(imgView);
            return borderImg;
        }

    }//end of: class GalleryAdapter


    /**
     * Helper Functions
     * @throws IOException 
     */
    private Bitmap getBitmapFromAsset(String strName) throws IOException
    {
        AssetManager assetManager = getAssets();

        InputStream istr = assetManager.open(strName);
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        istr.close();

        return bitmap;
    }
}

您将这些图片放在
资产/
中而不是作为可绘制资源,这有什么特别的原因吗?实际上,我想动态加载这些图片,它们的数量很多。当我把它们放在资源中时,我的内存有问题。ContentProvider有点失控,所以我想我应该在资产中试用。-Saranby顺便说一句,因为您希望动态加载图像(在我的示例中,我对它们进行了硬编码),如果我是您,我会将图像放在“资产”文件夹中,然后使用am.list(“[您的文件夹名称]”);这是因为除了我自己的文件外,我还看到了资产中的Android文件getBitmapFromAsset()方法忽略了关闭它打开的InputStream。
public class myActivitye extends Activity
{
    private Gallery mGallery;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mGallery = (Gallery) findViewById(R.id.mygalleryinxml);

        //load images into memory
        mBitArray = new Bitmap[4];
        try
        {
            //these images are stored in the root of "assets"
            mBitArray[0] = getBitmapFromAsset("pic1.png");
            mBitArray[1] = getBitmapFromAsset("pic2.png");
            mBitArray[2] = getBitmapFromAsset("pic3.png");
            mBitArray[3] = getBitmapFromAsset("pic4.png");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        mGallery.setAdapter(new GalleryAdapter(this, mBitArray));
    }

    public class GalleryAdapter extends BaseAdapter
    {
        //member variables
        private Context mContext;
        private Bitmap[] mImageArray;

        //constructor
        public GalleryAdapter(Context context, Bitmap[] imgArray)
        {
            mContext = context;
            mImageArray = imgArray;
        }

        public int getCount()
        {
            return mImageArray.length;
        }

        public Object getItem(int position)
        {
            return position;
        }

        public long getItemId(int position)
        {
            return position;
        }

        //returns the individual images to the widget as it requires them
        public View getView(int position, View convertView, ViewGroup parent)
        {
            final ImageView imgView = new ImageView(mContext);

            imgView.setImageBitmap(mImageArray[position]);

            //put black borders around the image
            final RelativeLayout borderImg = new RelativeLayout(mContext);
            borderImg.setPadding(20, 20, 20, 20);
            borderImg.setBackgroundColor(0xff000000);//black
            borderImg.addView(imgView);
            return borderImg;
        }

    }//end of: class GalleryAdapter


    /**
     * Helper Functions
     * @throws IOException 
     */
    private Bitmap getBitmapFromAsset(String strName) throws IOException
    {
        AssetManager assetManager = getAssets();

        InputStream istr = assetManager.open(strName);
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        istr.close();

        return bitmap;
    }
}