如何从gridview的返回位置获取图像的Uri

时间:2012-10-27 20:00:31

标签: android

我目前有一个gridview,它将图像位置/ id返回给父活动。我还想将图像Uri返回或提取到父活动,并将其设置为可在父活动中的其他位置解析的变量。我一直在寻找,但是找不到适合我正在尝试的解决方案。

我的gridview子活动返回位置

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

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    /**
     * On Click event for Single Gridview Item
     * */
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {


            Intent data = new Intent(PicPicker2.this, PictureActivity.class);
            // passing array index
            data.putExtra("id", position);
            setResult(Activity.RESULT_OK, data);
            finish();
        }
    });
}

}

我目前在父活动中捕获结果

            else if (reqCode == 2) {


                // Selected image id
                int position = data.getExtras().getInt("id");
                ImageAdapter imageAdapter = new ImageAdapter(this);
                ChosenImageView.setImageResource(imageAdapter.mThumbIds[position]);
              }
            }
        }     

我想要解析变量

        Uri attached_Uri = Uri.parse("SOME VARIABLE OF THE RETURNED URI");

我想将Uri attached_Uri = Uri.parse()设置为在gridview中单击的项目。我只是一个周末的Android修补工,试图学习平台的概念,所以解释和例子对我来说有很长的路要走,非常感谢。

添加了我的imageadapter

公共类ImageAdapter扩展了BaseAdapter {

private Context mContext;


// Keep all Images in array
public Integer[] mThumbIds = {
        R.drawable.pic_1, R.drawable.pic_2,
        R.drawable.pic_3, R.drawable.pic_4,
        R.drawable.pic_5, R.drawable.pic_6,
        R.drawable.pic_7, R.drawable.pic_8,
        R.drawable.pic_9, R.drawable.pic_10,
        R.drawable.pic_11, R.drawable.pic_12,
        R.drawable.pic_13, R.drawable.pic_14,
        R.drawable.pic_15
};

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

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

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
    return imageView;
}

}

1 个答案:

答案 0 :(得分:1)

您使用资源作为图片,因此uris的格式为"android.resource://your.package.name/resource_id"。更具体地说,假设您拥有变量position中的图像索引(就像在onClick方法中一样),并且您有一个ImageAdapter的实例mImageAdapter(请注意,在您的情况下,您可以创建适配器的新副本,但通常您需要使用公共实例)。然后,以下内容将为您提供Uri

Uri imageUri = Uri.parse("android.resource://your.package.name/" + 
     mImageAdapter.getItem(position));

在上面的代码"your.package.name"中是AndroidManifest.xml文件中定义的包的名称。