0

我正在开发一个 android 应用程序,我正在尝试访问 android 内置的 Gallery 应用程序。我正在使用下面的代码。

        public void initialize() 
        {
           images.clear();
           final String[] columns = { MediaStore.Images.Thumbnails._ID };
           final String orderBy = MediaStore.Images.Media._ID;
           Cursor imagecursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                null, null, orderBy);

        if(imagecursor != null)
        {
            int image_column_index = imagecursor
                    .getColumnIndex(MediaStore.Images.Media._ID);
            int count = imagecursor.getCount();
            for (int i = 0; i < count; i++) 
            {
                imagecursor.moveToPosition(i);
                int id = imagecursor.getInt(image_column_index);
                ImageItem imageItem = new ImageItem();
                imageItem.id = id;
                lastId = id;
                imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
                        getApplicationContext().getContentResolver(), id,
                        MediaStore.Images.Thumbnails.MICRO_KIND, null);
                images.add(imageItem);
            }
            imagecursor.close();
        }
        notifyDataSetChanged();
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView
                    .findViewById(R.id.thumbImage);
            convertView.setTag(holder);
        }
        else 
        {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageItem item = images.get(position);
        holder.imageview.setId(position);
        holder.imageview.setOnClickListener(new OnClickListener()
        {

            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                int id = v.getId();
                ImageItem item = images.get(id);
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                final String[] columns = { MediaStore.Images.Media.DATA };
                Cursor imagecursor = managedQuery(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                        MediaStore.Images.Media._ID + " = " + item.id, null, MediaStore.Images.Media._ID);

                if (imagecursor != null && imagecursor.getCount() > 0)
                {
                    Log.e("image cursor","image cursor");
                    imagecursor.moveToPosition(0);
                    String path = imagecursor.getString(imagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                    File file = new File(path);

                    Log.e("image cursor","image cursor"+imagecursor);

                    imagecursor.close();
                    intent.setDataAndType(
                            Uri.fromFile(file),
                            "image/*");
                    startActivityForResult(intent, VIEW_IMAGE);
                }
            }
        });
        holder.imageview.setImageBitmap(item.img);
        return convertView;
    }

该代码适用于所有设备,除了 micromax funbook p300 选项卡。错误日志看起来像这样

http://textuploader.com/?p=6&id=AkS9T

没有得到我要去哪里错了!请帮忙!谢谢!

4

1 回答 1

0

它抛出以下异常:

android.database.StaleDataException:试图在游标关闭后访问它。

考虑imagecursor.close()进入onDestroy()Activity 的方法。

这篇文章中的其他相关提示。

于 2013-09-25T10:26:53.443 回答