Reputation: 97
I currently have a gridview that returns an images position/id to a parent activity. I would also like to return or extract the images Uri to the parent activity and set it to a variable that can be parsed somewhere else in the parent activity. I've been looking for a bit but cant quite find a solution that fits what I'm attempting.
My gridview child activity that returns the position
@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();
}
});
}
}
Where I currently catch the result in the parent activity
else if (reqCode == 2) {
// Selected image id
int position = data.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ChosenImageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
}
Where I want to parse the variable
Uri attached_Uri = Uri.parse("SOME VARIABLE OF THE RETURNED URI");
I want to set Uri attached_Uri = Uri.parse() to the item that was clicked in the gridview. I'm just a weekend Android tinkerer trying to learn the concepts of the platform so explanations and examples go a long way for me and are greatly appreciated.
Added my imageadapter
public class ImageAdapter extends 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;
}
}
Upvotes: 0
Views: 959
Reputation: 6198
You're using resources as images, so the uris are of the form "android.resource://your.package.name/resource_id"
. To be more specific, lets say you have the index of the image in a variable position
(as you would in the onClick
method), and you've got an instance of ImageAdapter
in mImageAdapter
(note that in your case you could just create a new copy of the adapter, but generally you'd want to use a common instance). Then the following will get you your Uri
Uri imageUri = Uri.parse("android.resource://your.package.name/" +
mImageAdapter.getItem(position));
In the above code "your.package.name"
is the name of the package as defined in your AndroidManifest.xml
file.
Upvotes: 1