【问题标题】:android - create gallery with photos from sdcardandroid - 使用 sdcard 中的照片创建图库
【发布时间】:2012-03-25 07:58:48
【问题描述】:

我正在尝试使用 sdcard 中的照片创建图库。我有一个数据库表。表中的一列有一张照片的路径。

我遵循此链接作为指导,但此示例代码从 res/drawable 目录中检索图像 ID。我不确定如何修改它以使我的代码正常工作。

http://saigeethamn.blogspot.com/2010/05/gallery-view-android-developer-tutorial.html

但如前所述,我想使用数据库表中存在的任何图像来显示照片。以下代码为 ImageAdapter。

    public class ImageAdapter extends BaseAdapter {

    private Context ctx;

    int imageBackground;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.milestone_style);
        imageBackground = ta.getResourceId(R.styleable.milestone_style_android_galleryItemBackground, 1);
        ta.recycle();
    }

    @Override
    public int getCount() {
        return imagePhotosLocations.length;
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        if (milestones != null && !milestones.isEmpty()) {
            Milestone milestone = milestones.get(0);
            String imageFileLocation = milestone.getFileLocation();
            System.out.println("imageFileLocation="+imageFileLocation);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 26;
            Bitmap b;
            ImageView iv = new ImageView(ctx);
            try {
                if (imageFileLocation.startsWith("content://")) {
                    b = BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(imageFileLocation)), null, options);
                } else {
                    b = BitmapFactory.decodeFile(imageFileLocation, options);
                }
                iv.setImageBitmap(b);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
                // milestoneImageView.setBackgroundResource(imageBackground);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            return iv;
        }
        return null;
    }
}

这是活动中的方法之一。

private String[] imagePhotosLocations;

private void init() {
    milestones = getMilestones();
    imagePhotosLocations = new String[milestones.size()];
    int index = 0;
    for (Milestone milestone : milestones) {
        imagePhotosLocations[index++] = milestone.getFileLocation();
    }
}

private void initializeGallery() {
    milestoneImageView = (ImageView) this.findViewById(R.id.imagePhoto);
    Gallery milestoneGallery = (Gallery) this.findViewById(R.id.milestoneGallery);
    if (milestoneGallery == null) {
        throw new IllegalArgumentException("milestoneGallery can not be null.");
    }
    milestoneGallery.setAdapter(new ImageAdapter(this));
    milestoneGallery.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int which, long arg3) {
            String imageFileLocation = imagePhotosLocations[which];
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 26;
            Bitmap b;
            try {
                if (imageFileLocation.startsWith("content://")) {
                    b = BitmapFactory.decodeStream(getContentResolver().openInputStream(Uri.parse(imageFileLocation)), null, options);
                } else {
                    b = BitmapFactory.decodeFile(imageFileLocation, options);
                }
                milestoneImageView.setImageBitmap(b);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    });
}

【问题讨论】:

    标签: android


    【解决方案1】:

    这是我使用的步骤,

    1. 首先检查SD卡是否挂载。
    2. 查询您的数据库。可在此处找到详细信息:
      SQLite tutorial
    3. 查询返回结果集的游标。
    4. 使用自定义光标适配器(参考:Custom Cursor Adapter
    5. 有时如果图像太大,将图像加载到位图中可能会超出 VM 预算。所以参考这篇文章Avoiding out of memory issue while loading image

    6. 所以光标适配器返回一个图像视图,就像它在您的代码中所做的那样。

    7. 最终将此自定义适配器绑定到您的相册。

    希望这能回答你的问题。

    【讨论】:

    • 谢谢。我会试一试的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多