获取带图像的 gridview

发布于 2024-12-04 02:06:23 字数 2994 浏览 2 评论 0原文

我正在尝试将图像从 SD 卡获取到网格视图中,但我只想获取相机文件。这是我的代码:

public class GrillaActivity extends Activity 
{
    private Cursor imagecursor, actualimagecursor;
    private int image_column_index, actual_image_column_index;
    GridView imagegrid;
    private int count;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_view);
    init_phone_image_grid();
}

private void init_phone_image_grid() 
{
    String[] img = { MediaStore.Images.Thumbnails._ID };
    imagecursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, MediaStore.Images.Thumbnails.DATA + "='/sdcard/DCIM/Camera/*.*'", null, MediaStore.Images.Thumbnails.IMAGE_ID + "");

    image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
    count = imagecursor.getCount();
    imagegrid = (GridView)findViewById(R.id.PhoneImageGrid);
    imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
    imagegrid.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView parent, View v, int position, long id) 
        {
            System.gc();
            String[] proj = { MediaStore.Images.Media.DATA };
            actualimagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Images.Media.DATA + "='/sdcard/DCIM/Camera/*.*'", null, null);
            actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            actualimagecursor.moveToPosition(position);
            String i = actualimagecursor.getString(actual_image_column_index);
            System.gc();

            Intent objI = getIntent();
            objI.putExtra("clickedFile", i);
            setResult(100, objI);
            finish();
        }
    });
}

public class ImageAdapter extends BaseAdapter 
{
    private Context mContext;

    public ImageAdapter(Context c) 
    {
        mContext = c;
    }

    public int getCount() 
    {
        return count;
    }

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

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

    public View getView(int position,View convertView,ViewGroup parent) 
    {
        System.gc();
        ImageView i = new ImageView(mContext.getApplicationContext());
        if (convertView == null) 
        {
            imagecursor.moveToPosition(position);
            int id = imagecursor.getInt(image_column_index);

            i.setImageURI(Uri.withAppendedPath( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + id));

            i.setScaleType(ImageView.ScaleType.CENTER_CROP);
            i.setLayoutParams(new GridView.LayoutParams(92, 92));
        }
        else{
            i = (ImageView) convertView;
        }
        return i;
    }
}

}

我在 ManagedQuery 函数中添加了一个 SELECTION,但出现错误。 所以请帮助我,也许我做错了什么。提前致谢。

i am trying to get the images into a gridview from the sdcard, but i just want to get ONLY the CAMERA files. So this is my code:

public class GrillaActivity extends Activity 
{
    private Cursor imagecursor, actualimagecursor;
    private int image_column_index, actual_image_column_index;
    GridView imagegrid;
    private int count;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_view);
    init_phone_image_grid();
}

private void init_phone_image_grid() 
{
    String[] img = { MediaStore.Images.Thumbnails._ID };
    imagecursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, MediaStore.Images.Thumbnails.DATA + "='/sdcard/DCIM/Camera/*.*'", null, MediaStore.Images.Thumbnails.IMAGE_ID + "");

    image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
    count = imagecursor.getCount();
    imagegrid = (GridView)findViewById(R.id.PhoneImageGrid);
    imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
    imagegrid.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView parent, View v, int position, long id) 
        {
            System.gc();
            String[] proj = { MediaStore.Images.Media.DATA };
            actualimagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, MediaStore.Images.Media.DATA + "='/sdcard/DCIM/Camera/*.*'", null, null);
            actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            actualimagecursor.moveToPosition(position);
            String i = actualimagecursor.getString(actual_image_column_index);
            System.gc();

            Intent objI = getIntent();
            objI.putExtra("clickedFile", i);
            setResult(100, objI);
            finish();
        }
    });
}

public class ImageAdapter extends BaseAdapter 
{
    private Context mContext;

    public ImageAdapter(Context c) 
    {
        mContext = c;
    }

    public int getCount() 
    {
        return count;
    }

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

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

    public View getView(int position,View convertView,ViewGroup parent) 
    {
        System.gc();
        ImageView i = new ImageView(mContext.getApplicationContext());
        if (convertView == null) 
        {
            imagecursor.moveToPosition(position);
            int id = imagecursor.getInt(image_column_index);

            i.setImageURI(Uri.withAppendedPath( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + id));

            i.setScaleType(ImageView.ScaleType.CENTER_CROP);
            i.setLayoutParams(new GridView.LayoutParams(92, 92));
        }
        else{
            i = (ImageView) convertView;
        }
        return i;
    }
}

}

I had added a SELECTION in the managedQuery function, but i'm getting an error.
So please help me, maybe i'm doing something wrong. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文