Getting images from SD-CARD for a custom Dialog
What I am trying to do:: I am trying to learn to use dialogs, OnClick of imageview i want to display a custom dialog which has images stored in sdcard. then on-select of any one image, I want to set the image for imageview
What i have done:: I have achieved above specifications for images from drawable
DataAcceptActivity.java
public class DataAcceptActivity extends Activity {
InputStream is;
EditText name;
ImageView imageView;
int[] image_array={R.drawable.index,R.drawable.image1,R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8,R.drawable.image9,R.drawable.image10};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showImages();
}
});
}
protected void showImages() {
// TODO Auto-generated method stub
final Dialog dlg=new Dialog(DataAcceptActivity.this);
dlg.setContentView(R.layout.grid_view);
GridView GV=(GridView) dlg.findViewById(R.id.gridView_id);
GV.setAdapter(new ImageAdapter(DataAcceptActivity.this));
GV.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
imageView.setImageResource(image_array[arg2]);
dlg.dismiss();
}
});
dlg.show();
}
class ImageAdapter extends BaseAdapter{
Context cxt;
public ImageAdapter(DataAcceptActivity dataAcceptActivity) {
// TODO Auto-generated constructor stub
this.cxt=dataAcceptActivity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return image_array.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView ;
if(convertView==null){
imageView=new ImageView(cxt);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(15, 15, 15, 15);
}else{
imageView=(ImageView) convertView;
}
imageView.setImageResource(image_array[position]);
return imageView;
}
}
}
How can i modify code to achieve my goals !
Thanks
This are Arraylist that hold the bitmap that create from image path
ArrayList<Bitmap> bitmaps= new ArrayList<Bitmap>();
This is your Image folder path
File file = new File(Environment.getExternalStoragePath()+"/Stampii/");
This is list of image in folder
file imageList[] = file.listFiles();
This code take image from path and store in bitmap arraylist.
for(int i=0;i<imageList.length;i++)
{
Log.e("Image: "+i+": path", imageList[i].getAbsolutePath());
Bitmap b = BitmapFactory.decodeFile(imageList[i].getAbsolutePath());
bitmaps.add(b);
}
Now, use this Bitmap array within int[] image_array and use imageView.setImageBitmap(bitmaps.get(position)); for imageView.setImageResource(image_array[position]);
Thanks
Related
I have a gallery with images. I want to open other activities every time I click an image from the gallery. I'm having a hard time with Onclicklisteners/onitemclicklisteners.
here's my code.
public class MainActivity extends ActionBarActivity {
private Integer[] pics = { R.drawable.school, R.drawable.gradeview, R.drawable.history, R.drawable.schoolorg };
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
imageView = (ImageView) findViewById(R.id.imageView1);
gallery.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//
// I DON'T KNOW WHAT TO PUT HERE
// I DON'T KNOW WHAT TO PUT HERE
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ImageAdapter extends BaseAdapter{
private Context context;
int imageBackground;
public ImageAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return pics.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(context);
imageView.setImageResource(pics[arg0]);
return imageView;
}
}
}
to open other activity, you can simpy user intent,
Intent intent = new Intent();
intent.setClass(getApplicationContext(), YOUR_NEW_ACTIVITY.class);
startActivity(intent);
but make sure, your new activity must be declared in manifest like:
<activity android:name=".YOUR_NEW_ACTIVITY"></activity>
gallery.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent =new Intent(MainActvity.this,ImageShow.class);//Class name to redirect
intent.putExtra("Image",pics[arg2]); //Image url to send to the next to show image
startActivity(intent);
this.finish();
}
});
Sorry for my english. There is a mini-gallery, when you click on an image from the gallery it should (picture) to open in full screen. App just throws by pressing the image. what am I doing wrong?
MainActivity
public class MainAcTwo extends Activity {
#SuppressWarnings("deprecation")
Gallery gallery;
ImageView bigimage;
#SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
gallery=(Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB) #SuppressLint("NewApi") public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
long imageId = ImageAdapter.ThumbsIds[position];
Intent fullScreenIntent = new Intent(v.getContext(), FullScreenImage.class);
fullScreenIntent.putExtra(MainAcTwo.class.getName(), imageId);
MainAcTwo.this.startActivity(fullScreenIntent);
}
});
}
}
ImageAdapter
public class ImageAdapter extends BaseAdapter implements SpinnerAdapter {
private Context context;
public ImageAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return ThumbsIds.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView=null;
if(convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(215, 200));
imageView.setPadding(8, 8, 8, 8);
}else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(ThumbsIds[position]);
return imageView;
}
public static Integer[] ThumbsIds={
R.drawable.abs_icla,
R.drawable.abs_dog,
R.drawable.abs_flow,
R.drawable.abs_neb,
R.drawable.abs_rad
};
}
FullScreenImage
public class FullScreenImage extends Activity {
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.full_image);
Intent intent = getIntent();
long imageId = (Long) intent.getExtras().get(FullScreenImage.class.getName());
ImageView imageView = (ImageView) findViewById(R.id.fullImage);
imageView.setLayoutParams( new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
imageView.setImageResource((int) imageId);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
clean you project if it does not work
imageView.setLayoutParams( new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
change it to
imageView.setLayoutParams( new LinearLayout.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
I have a gridview which I am inflating in a Dialog view.
Now when I open the dialog ( with Grid View ) it shows a set of 10 images.
when dialog is dismissed the image which I selected is set on the ImageView.
Now my query is can I get the name of the image?? which is set on the ImageView???
I have to save it somehwere.
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
final Dialog groupIconsDialog = new Dialog(CreateGroupActivity.this);
groupIconsDialog.setTitle("Choose Group Icon");
groupIconsDialog.setContentView(R.layout.group_icons_layout);
//calling and setting the image icons to the grid view adapter
GridView groupIconsGrid = (GridView)groupIconsDialog.findViewById(R.id.grid_groupIcons);
groupIconsGrid.setAdapter(new GroupIconAdapter(CreateGroupActivity.this));
groupIconsGrid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
group_icon.setImageResource(new GroupIconAdapter(CreateGroupActivity.this).mThumbIds[position]);
groupIconsDialog.dismiss();
}
});
groupIconsDialog.show();
}
});
ImageAdapter
public class GroupIconAdapter extends BaseAdapter {
private Context mContext;
//icons image array
public Integer[] mThumbIds = {
R.drawable.mogra,R.drawable.rhino,
R.drawable.zebra,R.drawable.lion,
R.drawable.mogra,R.drawable.rhino,
R.drawable.giraffee,R.drawable.giraffee,
R.drawable.lion,R.drawable.rhino
};
public GroupIconAdapter(Context c) {
// TODO Auto-generated constructor stub
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
return imageView;
}
}
Yes you can get the name of the Image if you have the resource id of the Image. You can use getResourceEntryName(resourceId)
String name = getResources().getResourceEntryName(mThumbIds[position]);
I am providing my code here.i have breaked an image into 9 parts and shown in grid view how to change images by clicking on them i.e.,(first clicked image should be replaced with second clicked image and vice versa)i have used bitmap array for splitting images and placed them in grid view. so how to change images in grid view by clicking two images the swapping of images should be done how can any one help me.
public class Imagepieces extends Activity {
ArrayList<Bitmap> breakedimages,duplicate;
GridView g;
int i=0,temp,temp2,rpos;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
breakedimages = getIntent().getParcelableArrayListExtra("breaked image");
duplicate = new ArrayList<Bitmap>(breakedimages);
Collections.shuffle(duplicate);
g = (GridView) findViewById(R.id.gridView1);
g.setAdapter(new CutAdapter(this, breakedimages));
g.setNumColumns((int) Math.sqrt(breakedimages.size()));
g.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//=================================================
{
}
});
}
class CutAdapter extends BaseAdapter {
int iwidth, iheight;
Context context;
public CutAdapter(Imagepieces ipieces, ArrayList<Bitmap> breakedimages) {
// TODO Auto-generated constructor stub
iwidth = breakedimages.get(0).getWidth();
iheight = breakedimages.get(0).getHeight();
context = ipieces;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return duplicate.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return duplicate.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView i=new ImageView(context);
i.setLayoutParams(new GridView.LayoutParams(iwidth +5,iheight +5));
i.setPadding(0, 0, 0, 0);
i.setImageBitmap(duplicate.get(arg0));
return i;
}
}
}
can any one do the need for me as i have stuck here how to move the images among themselves
first follow the #Terril Thomas's to select two image for swapping,
Here is the method for swap two Image in List,
public void swapImage(Bitmap i1,Bitmap i2){
int position1,position2;
position1 = duplicate.indexOf(i1);
duplicate.remove(position1);
position2 = duplicate.indexOf(i1);
duplicate.remove(position2);
if(position2>position1){
duplicate.add(position2, i1);
duplicate.add(position1, i2);
}
if(position2<position1){
duplicate.add(position1, i2);
duplicate.add(position2, i1);
}
}
change in onCreate like this
CutAdapter ca = new CutAdapter(this, breakedimages);
g.setAdapter(ca);
than refresh your adapter
ca.notifyDataSetChanged();
Another way we can implement.
In onItemClickListener()
i++;
Bitmap b = null;
if (i % 2 != 0) {
temp = arg2;
b = duplicate.get(temp);
}
if (i % 2 == 0) {
temp2 = arg2;
duplicate.set(temp, duplicate.get(arg2));
duplicate.set(temp2, b);
}
I have one GridView with 3 Column and 3 Rows I want to change Image when User Click any two Images.
for Example I Click First Row 1 and Column 3 Image and Secondly I Click on Row 3 and Column 2 show now i want to change this two Images like Swap the Image How is it Possible ?
public class MainActivity extends Activity {
/** Called when the activity is first created. */
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.gridviewmy);
gridView.setAdapter(new ImageAdapter(this));
final ImageAdapter im = new ImageAdapter(this);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
int i=0; int j=0;
if( i != 0){
j=arg2;
System.out.println("First Click "+j);
}else{
i=arg2;
System.out.println("Second Click "+i);
}
im.getItem(arg2);
//im.changeImage();
Toast.makeText(MainActivity.this, ""+arg2, Toast.LENGTH_SHORT).show();
System.out.println("AdapterView "+arg0);
System.out.println("View "+arg1);
System.out.println("Integer "+arg2);
System.out.println("long "+arg3);
}
});
}
}
class ImageAdapter extends BaseAdapter{
private Context mContext;
ImageView iView;
public ImageAdapter(Context c){
this.mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
System.out.println("Item Is :-"+mThumbIds[position].toString());
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
System.out.println("Geting Id of Item "+mThumbIds[position]);
if(iView != null){
iView.setImageResource(mThumbIds[0]);
Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
}
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if( convertView == null){
iView = new ImageView(mContext);
iView.setLayoutParams(new GridView.LayoutParams(85, 85));
iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
iView.setPadding(8,8,8,8);
}else{
iView = (ImageView)convertView;
}
iView.setImageResource(mThumbIds[position]);
return iView;
}
private Integer[] mThumbIds = {
R.drawable.a_bhaibij, R.drawable.a_dashera, R.drawable.a_dipawali,
R.drawable.a_gandhi, R.drawable.a_holi, R.drawable.a_indepe,
R.drawable.a_janmastmi, R.drawable.a_kite, R.drawable.a_newyear
};
public void changeImage(){
iView.setImageResource(mThumbIds[5]);
}
}
Swaping the images in the GridView is very simple.What you have to do is
1* Store the cliked position,where you want to perform the swaping .
2* By using those two values perform the swap operation on mThumbIds array.
3* Finally invoke the notifyDataSetChanged() method on the Adapter object i.e im.notifyDataSetChanged();
public class MainActivity extends Activity {
/** Called when the activity is first created. */
int i=0;
int firstClick,secondClick;
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.gridviewmy);
gridView.setAdapter(new ImageAdapter(this));
final ImageAdapter im = new ImageAdapter(this);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
i++;
if( i %2!=0){
firstClick=arg2;
}else{
secondClick=arg2;
Integer help=new Interger(mThumbIds[firstClick]);
mThumbIds[firstClick]=mThumbIds[secondClick];
mThumbIds[secondClick]=help;
notifyDataSetChanged();
System.out.println("Second Click "+i);
}
}
});
}
}
class ImageAdapter extends BaseAdapter{
private Context mContext;
ImageView iView;
public ImageAdapter(Context c){
this.mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
System.out.println("Item Is :-"+mThumbIds[position].toString());
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
System.out.println("Geting Id of Item "+mThumbIds[position]);
if(iView != null){
iView.setImageResource(mThumbIds[0]);
Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
}
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if( convertView == null){
iView = new ImageView(mContext);
iView.setLayoutParams(new GridView.LayoutParams(85, 85));
iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
iView.setPadding(8,8,8,8);
}else{
iView = (ImageView)convertView;
}
iView.setImageResource(mThumbIds[position]);
return iView;
}
private Integer[] mThumbIds = {
R.drawable.a_bhaibij, R.drawable.a_dashera, R.drawable.a_dipawali,
R.drawable.a_gandhi, R.drawable.a_holi, R.drawable.a_indepe,
R.drawable.a_janmastmi, R.drawable.a_kite, R.drawable.a_newyear
};
}
I think this may solve you problem.
All the best.
Also do the following for updating the grid view images to complete the swap operation:
im.notifyDataSetChanged();
gridView.setAdapter(im);
gridView.invalidateViews()
notifyDataSetChanged(); did not work for me. eclipse gave an error.
so instead of searching for the real solution, if there is one, I just reloaded the java page.
Of course I am saving the state of the images in the gridview (adapter) in internal storage in a file named graphics. So on reload of the java page it repaints with correct images.
It works.