基于数据库内的图像路径在gridview中显示图像

时间:2012-12-07 09:20:14

标签: android database sqlite gridview android-gridview

我在手机中保存了一些图像,并将图像路径插入数据库。 我想使用数据库中的图像路径并在gridview中显示它。 现在我可以创建一个gridview来在gallery app中显示图像。但我真的不知道如何根据数据库路径显示图像。如果可能,任何人都可以在这个问题上启发我吗?任何评论将不胜感激。

下面的代码我用来在图库中显示图像。

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.phone_tab);

    final String[] columns = { MediaColumns.DATA, BaseColumns._ID };
    final String orderBy = BaseColumns._ID;
    Cursor imagecursor = managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
            null, orderBy);
    int image_column_index = imagecursor.getColumnIndex(BaseColumns._ID);
    this.count = imagecursor.getCount();
    this.thumbnails = new Bitmap[this.count];
    this.arrPath = new String[this.count];
    this.thumbnailsselection = new boolean[this.count];
    for (int i = 0; i < this.count; i++) {
        imagecursor.moveToPosition(i);
        int id = imagecursor.getInt(image_column_index);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaColumns.DATA);
        thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                getApplicationContext().getContentResolver(), id,
                MediaStore.Images.Thumbnails.MICRO_KIND, null);
        arrPath[i]= imagecursor.getString(dataColumnIndex);
    }
    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);
    imagecursor.close();

    final Button selectBtn = (Button) findViewById(R.id.select_btn);
    selectBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            final int len = thumbnailsselection.length;
            int cnt = 0;
            String selectImages = "";
            for (int i =0; i<len; i++)
            {
                if (thumbnailsselection[i]){
                    cnt++;
                    selectImages = selectImages + arrPath[i] + "|";
                }
            }
            if (cnt == 0){
                Toast.makeText(getApplicationContext(),
                        "Please select at least one image",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "You've selected Total " + cnt + " image(s).",
                        Toast.LENGTH_LONG).show();
                Log.d("SelectedImages", selectImages);

                Intent input = new Intent(v.getContext(), InputScreen.class);
                input.putExtra("selectImages", selectImages+"");
                StringBuffer urlString = new StringBuffer();
                replaceContentView("InputScreen", input);

            }


        }



    });



}

值[2]返回为/mnt/sdcard/ImageFolder/Image1.jpg。

    String values[] = helper.get_user_image(useremail);
    String filepath = values[2];

1 个答案:

答案 0 :(得分:0)

最好在数据库中存储图像路径而不是图像。使用存储在数据库中的图像路径访问这些图像。

如果必须在数据库中使用图像

  • 请使用Stream而不是字节数组

以下资源可以帮助您

相关问题