如何在android中从gallery中选择多个图像?
我正在制作一个项目,我想从gallery中选择多张照片并将其保存在imageview阵列中。 我可以导入单个图像并保存在imageview中 谁能告诉我如何导入多个图像并保存在阵列或不同的ImageView中如何在android中从gallery中选择多个图像?,android,Android,我正在制作一个项目,我想从gallery中选择多张照片并将其保存在imageview阵列中。 我可以导入单个图像并保存在imageview中 谁能告诉我如何导入多个图像并保存在阵列或不同的ImageView中 MainActivity extends Activity implements OnClickListener { Button addphoto, save; ImageView img1[]; Bitmap yourbitmap, resized;
MainActivity extends Activity implements OnClickListener {
Button addphoto, save;
ImageView img1[];
Bitmap yourbitmap, resized;
int RESULT_LOAD_IMAGE1 =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addphoto = (Button)findViewById(R.id.add);
img1[0] = (ImageView)findViewById(R.id.imageView1);
save = (Button)findViewById(R.id.save);
addphoto.setOnClickListener(this);
save.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.add:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), RESULT_LOAD_IMAGE1);
break;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
try {
yourbitmap = BitmapFactory.decodeFile(picturePath);
resized= Bitmap.createScaledBitmap(yourbitmap, 200,300, true);
img1[0].setImageBitmap(resized);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/scroll1"
>
<LinearLayout
android:id="@+id/lnrImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout">
<Button
android:id="@+id/btnAddPhots"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Add Phots"/>
<Button
android:id="@+id/btnSaveImages"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
<ImageView
android:id="@+id/imgThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<CheckBox
android:id="@+id/chkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"/>
</FrameLayout>
通过
intent.putExtra()
方法在intent上设置了EXTRA\u ALLOW\u MULTIPLE
选项:
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/scroll1"
>
<LinearLayout
android:id="@+id/lnrImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout">
<Button
android:id="@+id/btnAddPhots"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Add Phots"/>
<Button
android:id="@+id/btnSaveImages"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
<ImageView
android:id="@+id/imgThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<CheckBox
android:id="@+id/chkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"/>
</FrameLayout>
在您的代码中,请按以下方式编写:
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), RESULT_LOAD_IMAGE1);
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/scroll1"
>
<LinearLayout
android:id="@+id/lnrImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout">
<Button
android:id="@+id/btnAddPhots"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Add Phots"/>
<Button
android:id="@+id/btnSaveImages"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
<ImageView
android:id="@+id/imgThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<CheckBox
android:id="@+id/chkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"/>
</FrameLayout>
注意:仅在AndroidAPI 18及更高版本中提供EXTRA\u ALLOW\u MULTIPLE
选项
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/scroll1"
>
<LinearLayout
android:id="@+id/lnrImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout">
<Button
android:id="@+id/btnAddPhots"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Add Phots"/>
<Button
android:id="@+id/btnSaveImages"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
<ImageView
android:id="@+id/imgThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<CheckBox
android:id="@+id/chkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"/>
</FrameLayout>
如果您想开发自己的多媒体资料,请使用以下代码查看“实现自定义多媒体资料选择器”:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/scroll1"
>
<LinearLayout
android:id="@+id/lnrImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout">
<Button
android:id="@+id/btnAddPhots"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Add Phots"/>
<Button
android:id="@+id/btnSaveImages"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
<ImageView
android:id="@+id/imgThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<CheckBox
android:id="@+id/chkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"/>
</FrameLayout>
主要活动
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/scroll1"
>
<LinearLayout
android:id="@+id/lnrImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout">
<Button
android:id="@+id/btnAddPhots"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Add Phots"/>
<Button
android:id="@+id/btnSaveImages"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
<ImageView
android:id="@+id/imgThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<CheckBox
android:id="@+id/chkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"/>
</FrameLayout>
自定义图片库
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/scroll1"
>
<LinearLayout
android:id="@+id/lnrImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout">
<Button
android:id="@+id/btnAddPhots"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Add Phots"/>
<Button
android:id="@+id/btnSaveImages"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<GridView
android:id="@+id/grdImages"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
<Button
android:id="@+id/btnSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"
android:layout_marginTop="5dp"/>
</LinearLayout>
<ImageView
android:id="@+id/imgThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<CheckBox
android:id="@+id/chkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"/>
</FrameLayout>
public class MainActivity extends Activity implements View.OnClickListener{
private LinearLayout lnrImages;
private Button btnAddPhots;
private Button btnSaveImages;
private ArrayList<String> imagesPathList;
private Bitmap yourbitmap;
private Bitmap resized;
private final int PICK_IMAGE_MULTIPLE =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lnrImages = (LinearLayout)findViewById(R.id.lnrImages);
btnAddPhots = (Button)findViewById(R.id.btnAddPhots);
btnSaveImages = (Button)findViewById(R.id.btnSaveImages);
btnAddPhots.setOnClickListener(this);
btnSaveImages.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnAddPhots:
Intent intent = new Intent(MainActivity.this,CustomPhotoGalleryActivity.class);
startActivityForResult(intent,PICK_IMAGE_MULTIPLE);
break;
case R.id.btnSaveImages:
if(imagesPathList !=null){
if(imagesPathList.size()>1) {
Toast.makeText(MainActivity.this, imagesPathList.size() + " no of images are selected", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, imagesPathList.size() + " no of image are selected", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this," no images are selected", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode == PICK_IMAGE_MULTIPLE){
imagesPathList = new ArrayList<String>();
String[] imagesPath = data.getStringExtra("data").split("\\|");
try{
lnrImages.removeAllViews();
}catch (Throwable e){
e.printStackTrace();
}
for (int i=0;i<imagesPath.length;i++){
imagesPathList.add(imagesPath[i]);
yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(yourbitmap);
imageView.setAdjustViewBounds(true);
lnrImages.addView(imageView);
}
}
}
}
}
public class CustomPhotoGalleryActivity extends Activity {
private GridView grdImages;
private Button btnSelect;
private ImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
/**
* Overrides methods
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_gallery);
grdImages= (GridView) findViewById(R.id.grdImages);
btnSelect= (Button) findViewById(R.id.btnSelect);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
@SuppressWarnings("deprecation")
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.arrPath = new String[this.count];
ids = new int[count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
ids[i] = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
imageAdapter = new ImageAdapter();
grdImages.setAdapter(imageAdapter);
imagecursor.close();
btnSelect.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
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 {
Log.d("SelectedImages", selectImages);
Intent i = new Intent();
i.putExtra("data", selectImages);
setResult(Activity.RESULT_OK, i);
finish();
}
}
});
}
@Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
/**
* Class method
*/
/**
* This method used to set bitmap.
* @param iv represented ImageView
* @param id represented id
*/
private void setBitmap(final ImageView iv, final int id) {
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
iv.setImageBitmap(result);
}
}.execute();
}
/**
* List adapter
* @author tasol
*/
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) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.custom_gallery_item, null);
holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
holder.chkImage = (CheckBox) convertView.findViewById(R.id.chkImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.chkImage.setId(position);
holder.imgThumb.setId(position);
holder.chkImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
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.imgThumb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = holder.chkImage.getId();
if (thumbnailsselection[id]) {
holder.chkImage.setChecked(false);
thumbnailsselection[id] = false;
} else {
holder.chkImage.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
try {
setBitmap(holder.imgThumb, ids[position]);
} catch (Throwable e) {
}
holder.chkImage.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
/**
* Inner class
* @author tasol
*/
class ViewHolder {
ImageView imgThumb;
CheckBox chkImage;
int id;
}
}
自定义\u图库\u项目
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/scroll1"
>
<LinearLayout
android:id="@+id/lnrImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout">
<Button
android:id="@+id/btnAddPhots"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Add Phots"/>
<Button
android:id="@+id/btnSaveImages"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
<ImageView
android:id="@+id/imgThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<CheckBox
android:id="@+id/chkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"/>
</FrameLayout>
main活动
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/scroll1"
>
<LinearLayout
android:id="@+id/lnrImages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/linearLayout">
<Button
android:id="@+id/btnAddPhots"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Add Phots"/>
<Button
android:id="@+id/btnSaveImages"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<GridView
android:id="@+id/grdImages"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
<Button
android:id="@+id/btnSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"
android:layout_marginTop="5dp"/>
</LinearLayout>
<ImageView
android:id="@+id/imgThumb"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<CheckBox
android:id="@+id/chkImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"/>
</FrameLayout>
public class MainActivity extends Activity implements View.OnClickListener{
private LinearLayout lnrImages;
private Button btnAddPhots;
private Button btnSaveImages;
private ArrayList<String> imagesPathList;
private Bitmap yourbitmap;
private Bitmap resized;
private final int PICK_IMAGE_MULTIPLE =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lnrImages = (LinearLayout)findViewById(R.id.lnrImages);
btnAddPhots = (Button)findViewById(R.id.btnAddPhots);
btnSaveImages = (Button)findViewById(R.id.btnSaveImages);
btnAddPhots.setOnClickListener(this);
btnSaveImages.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnAddPhots:
Intent intent = new Intent(MainActivity.this,CustomPhotoGalleryActivity.class);
startActivityForResult(intent,PICK_IMAGE_MULTIPLE);
break;
case R.id.btnSaveImages:
if(imagesPathList !=null){
if(imagesPathList.size()>1) {
Toast.makeText(MainActivity.this, imagesPathList.size() + " no of images are selected", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, imagesPathList.size() + " no of image are selected", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this," no images are selected", Toast.LENGTH_SHORT).show();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode == PICK_IMAGE_MULTIPLE){
imagesPathList = new ArrayList<String>();
String[] imagesPath = data.getStringExtra("data").split("\\|");
try{
lnrImages.removeAllViews();
}catch (Throwable e){
e.printStackTrace();
}
for (int i=0;i<imagesPath.length;i++){
imagesPathList.add(imagesPath[i]);
yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(yourbitmap);
imageView.setAdjustViewBounds(true);
lnrImages.addView(imageView);
}
}
}
}
}
public class CustomPhotoGalleryActivity extends Activity {
private GridView grdImages;
private Button btnSelect;
private ImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
/**
* Overrides methods
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_gallery);
grdImages= (GridView) findViewById(R.id.grdImages);
btnSelect= (Button) findViewById(R.id.btnSelect);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
@SuppressWarnings("deprecation")
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.arrPath = new String[this.count];
ids = new int[count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
ids[i] = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
imageAdapter = new ImageAdapter();
grdImages.setAdapter(imageAdapter);
imagecursor.close();
btnSelect.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
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 {
Log.d("SelectedImages", selectImages);
Intent i = new Intent();
i.putExtra("data", selectImages);
setResult(Activity.RESULT_OK, i);
finish();
}
}
});
}
@Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
/**
* Class method
*/
/**
* This method used to set bitmap.
* @param iv represented ImageView
* @param id represented id
*/
private void setBitmap(final ImageView iv, final int id) {
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
iv.setImageBitmap(result);
}
}.execute();
}
/**
* List adapter
* @author tasol
*/
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) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.custom_gallery_item, null);
holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
holder.chkImage = (CheckBox) convertView.findViewById(R.id.chkImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.chkImage.setId(position);
holder.imgThumb.setId(position);
holder.chkImage.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
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.imgThumb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = holder.chkImage.getId();
if (thumbnailsselection[id]) {
holder.chkImage.setChecked(false);
thumbnailsselection[id] = false;
} else {
holder.chkImage.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
try {
setBitmap(holder.imgThumb, ids[position]);
} catch (Throwable e) {
}
holder.chkImage.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
/**
* Inner class
* @author tasol
*/
class ViewHolder {
ImageView imgThumb;
CheckBox chkImage;
int id;
}
}
public类MainActivity扩展活动实现View.OnClickListener{
私人线路布局图;
私人按钮btnAddPhots;
私人按钮btnsaviemages;
私有ArrayList图像列表;
私有位图;
私有位图的大小;
私有最终整数选取图像倍数=1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lnrImages=(线性布局)findViewById(R.id.lnrImages);
btnAddPhots=(按钮)findViewById(R.id.btnAddPhots);
btnsaviemages=(按钮)findViewById(R.id.btnsaviemages);
btnAddPhots.setOnClickListener(此);
btnsavimages.setOnClickListener(此);
}
@凌驾
公共void onClick(视图){
开关(view.getId()){
案例R.id.btnAddPhots:
意向意向=新意向(MainActivity.this、CustomPhotoGalleryActivity.class);
startActivityForResult(意图、拾取图像或倍数);
打破
案例R.id.btnsaviemages:
if(imagesPathList!=null){
如果(imagesPathList.size()>1){
Toast.makeText(MainActivity.this,imagesPathList.size()+“未选择任何图像”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(MainActivity.this,imagesPathList.size()+“未选择任何图像”,Toast.LENGTH_SHORT.show();
}
}否则{
Toast.makeText(MainActivity.this,“未选择任何图像”,Toast.LENGTH_SHORT.show();
}
打破
}
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
if(resultCode==RESULT\u OK){
if(requestCode==拾取图像\u倍数){
imagesPathList=newarraylist();
字符串[]imagesPath=data.getStringExtra(“数据”).split(“\\\\”);
试一试{
lnrImages.removeallview();
}捕获(可丢弃的e){
e、 printStackTrace();
}
对于(int i=0;i<代码>),这是应用程序的显式文件代码,以考虑您正在向应用程序发送图像。
程序代码。。。
公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(“主要活动”、“创建时”);
if(Intent.ACTION\u SEND\u MULTIPLE.equals(getIntent().getAction())和&getIntent().hasExtra(Intent.EXTRA\u流))
{
数组列表=
getIntent().getParcelableArrayListExtra(Intent.EXTRA_流);
用于(可包裹包裹:列表){
Uri uri1=(Uri)包;
Log.d(“MainActivity”,“on Create uri1”+uri1);
字符串sourcepath=getPath(uri1);
Log.d(“MainActivity”,“on Create sourcepath”+sourcepath);
///在这里对每个图像源路径执行操作。
}
//Aishwary评论,因为我想在选择图像后打开应用程序,否则取消注释以仅选择
//完成();
}否则{
//这部分是Aishway的单幅图像
Uri imageUri=(Uri)getIntent().getExtras().get(Intent.EXTRA_流);
Log.d(“MainActivity”,“关于创建ImageUri”+ImageUri);
}
}
公共字符串getPath(Uri){
字符串[]投影={MediaStore.Images.Media.DATA};
Cursor Cursor=managedQuery(uri、投影、null、null、null);
开始管理游标(游标);
int column_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(列索引);
}
}
这将允许您获取源路径和图像url。现在您可以为url创建数组并根据需要使用此url。。。
Intent Intent=new Intent();
intent.setType(“image/*”);
intent.setAction(intent.ACTION\u GET\u CONTENT);
startActivityForResult(Intent.createChooser(Intent,“选择图片”),选择图片);
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
试一试{
开关(请求代码){
案例选择图片:
Uri selectedImageUri=data.getData();
img