如何让用户将图像添加到系统中?

时间:2015-03-01 21:08:10

标签: android image

我遇到了问题。我正在使用Image GridView的东西,我创建了一个对象。我想通过点击按钮让用户将图像添加到资源,然后我想甚至能够使用该图像ID,这样我就可以将它添加到我的对象和gridview中。

这是我的活动:(需要的部分)

    public class ImageAdapter extends BaseAdapter {
    private Context mContext;

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

    public int getCount() {
        return mThumbIds.size();
    }

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid_single, null);
            ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
            TextView textView = (TextView) grid.findViewById(R.id.grid_text);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            textView.setText(mThumbIds.get(position).getDesc());
            imageView.setImageResource(mThumbIds.get(position).getImageID());
        } else {
            grid = (View) convertView;
        }
        return grid;
    }
}

public void createAll(){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    parentGender = prefs.getString("parentGender", "1");
    parentNum = prefs.getString("parentNum", "");
    childName = prefs.getString("childName", "Kiddo");
    final String opening=parentGender+", "+childName;
    if(mThumbIds.size()==0){
        mThumbIds.add(tv);
        mThumbIds.add(cookie);
        mThumbIds.add(toilet);
        mThumbIds.add(toys);
        mThumbIds.add(food);
        mThumbIds.add(water);
    }
    final GridView gridview = (GridView) findViewById(R.id.grid);
    gridview.setAdapter(new ImageAdapter(this));


    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
blah blah blah .....(从现在开始处理点击项目)

注意::: mThumbIds是我的对象的ArrayList<E> E>,其中包含标题和图片ID!

如何让用户添加图像并使用其ID?

1 个答案:

答案 0 :(得分:0)

如评论所述,您无法在运行时将任何图像保存到资源。您必须处理其他概念,例如将图像路径中的图像存储在sqlite数据库中。所以,如果我是你,首先我会实现一个模型类,它将描述你想要的所有功能。 所以,第一步:创建一个模型类:

public class MyPicture{
  String filePath, pictureId;
  Bitmap thumbNail, pictureBmp;
} /// constructor , setters, getters etc

然后,我将实现一种方法,从SD卡或MediaStore中为用户挑选图片,换句话说。

// first open an activity to browse the pictures from MediaStore and return the result with code CODE.
public void browsePictures(){
    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(i, CODE);
}

您可以查看有关startActivityForResult方法here的更多信息 然后收到结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CODE && resultCode == RESULT_OK && null != data) { // we have bitmap from filesystem!
        Uri selectedImage = data.getData();

        InputStream inputStream = null;

        if (ContentResolver.SCHEME_CONTENT.equals(selectedImage.getScheme())) {
            try {
                inputStream = this.getContentResolver().openInputStream(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            if (ContentResolver.SCHEME_FILE.equals(selectedImage.getScheme())) {
                try {
                    inputStream = new FileInputStream(selectedImage.getPath());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }

        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        // here you have to store the data that you received. In other words , 
       // create the MyPicture object and then store it to your sqlite db.
    }

}

有关onActivityResult方法的更多信息,请查看here。 正如您可以在文档页面中看到的那样,startActivityForResult和onActivityResult是一对必须一起使用的方法。变量&#39; CODE&#39;是帮助我们区分onActivityResult方法中收到的多个结果的变量。

有很多关于设置sqlite数据库的说明的教程。 您可以查看official documentation