Some times we may require to select multiple images from gallery. So using the following method you will be able to select multiple images. Additionally it will also remove gallery app dependency.
It may not be sufficient method as I need to maintain two array in this activity, but it could vary on requirements. Please comment if you have any suggestion in code.
Main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < Button android:id = "@+id/selectBtn" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Select" android:layout_alignParentBottom = "true" android:layout_centerHorizontal = "true" android:minWidth = "200px" /> < GridView android:id = "@+id/PhoneImageGrid" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:numColumns = "auto_fit" android:verticalSpacing = "10dp" android:horizontalSpacing = "10dp" android:columnWidth = "90dp" android:stretchMode = "columnWidth" android:gravity = "center" android:layout_above = "@id/selectBtn" /> </ RelativeLayout > |
galleryitem.xml
1
2
3
4
5
6
7
8
9
|
<? xml version = "1.0" encoding = "utf-8" ?> android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < ImageView android:id = "@+id/thumbImage" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" /> < CheckBox android:id = "@+id/itemCheckBox" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentRight = "true" android:layout_alignParentTop = "true" /> </ RelativeLayout > |
AndroidCustomGalleryActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
package com.isummation.customgallery; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.GridView; import android.widget.ImageView; import android.widget.Toast; public class AndroidCustomGalleryActivity extends Activity { private int count; private Bitmap[] thumbnails; private boolean [] thumbnailsselection; private String[] arrPath; private ImageAdapter imageAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }; final String orderBy = MediaStore.Images.Media._ID; Cursor imagecursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null , null , orderBy); int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID); this .count = imagecursor.getCount(); this .thumbnails = new Bitmap[ this .count]; this .arrPath = new String[ this .count]; this .thumbnailsselection = new boolean [ this .count]; for ( int i = 0 ; i < this .count; i++) { imagecursor.moveToPosition(i); int id = imagecursor.getInt(image_column_index); int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA); thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail( getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null ); arrPath[i]= imagecursor.getString(dataColumnIndex); } GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid); imageAdapter = new ImageAdapter(); imagegrid.setAdapter(imageAdapter); imagecursor.close(); final Button selectBtn = (Button) findViewById(R.id.selectBtn); selectBtn.setOnClickListener( new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub final int len = thumbnailsselection.length; int cnt = 0 ; String selectImages = "" ; for ( int i = 0 ; i<len; i++) { if (thumbnailsselection[i]){ cnt++; selectImages = selectImages + arrPath[i] + "|" ; } } if (cnt == 0 ){ Toast.makeText(getApplicationContext(), "Please select at least one image" , Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "You've selected Total " + cnt + " image(s)." , Toast.LENGTH_LONG).show(); Log.d( "SelectedImages" , selectImages); } } }); } public class ImageAdapter extends BaseAdapter { private LayoutInflater mInflater; public ImageAdapter() { mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { return count; } public Object getItem( int position) { return position; } public long getItemId( int position) { return position; } public View getView( int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null ) { holder = new ViewHolder(); convertView = mInflater.inflate( R.layout.galleryitem, null ); holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage); holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.checkbox.setId(position); holder.imageview.setId(position); holder.checkbox.setOnClickListener( new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub CheckBox cb = (CheckBox) v; int id = cb.getId(); if (thumbnailsselection[id]){ cb.setChecked( false ); thumbnailsselection[id] = false ; } else { cb.setChecked( true ); thumbnailsselection[id] = true ; } } }); holder.imageview.setOnClickListener( new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub int id = v.getId(); Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); startActivity(intent); } }); holder.imageview.setImageBitmap(thumbnails[position]); holder.checkbox.setChecked(thumbnailsselection[position]); holder.id = position; return convertView; } } class ViewHolder { ImageView imageview; CheckBox checkbox; int id; } } |
Update: need more features? see my open source project
Update: Please remove code that closes cursor. As this will not work in Android 4.0 and newer.
Test 2 :
Get all gallary image and store it in parse.com table
Simple use following java file :
public class AndroidCustomGalleryActivity extends Activity {
private int count;
private Bitmap[] thumbnails;
private boolean[] thumbnailsselection;
private String[] arrPath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
arrPath[i]= imagecursor.getString(dataColumnIndex);
Bitmap image = thumbnails[i];
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] data = stream.toByteArray();
ParseFile imgFile = new ParseFile (“Image”, data);
imgFile.saveInBackground();
//Info in Parse Dashboard
ParseObject imageObj = new ParseObject (“Image”);
imageObj.put(“Image”, imgFile);
imageObj.saveInBackground();
Toast.makeText(getApplicationContext(),”over”,Toast.LENGTH_LONG).show();
}
imagecursor.close();
}
}
it store all your gallary image on your parse.com table.