如何像whatsapp

时间:2015-12-04 10:48:55

标签: java android image android-gallery whatsapp

我想浏览android gallery以选择像Whatsapp这样的多个图像将其发送到服务器。

我已经阅读了很多文章,但没有得到解决方案。 现在我用这个:

MainActivity

public class MainActivity extends Activity{
private ImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
private final Logger log = Logger.getAnonymousLogger();
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
}
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){
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();
}
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();
}
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.photo_gallery_item, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
log.info("Holder "+ holder);
holder.cb = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
holder.cb.setId(position);
holder.imageview.setId(position);
holder.cb.setOnClickListener(new OnClickListener(){
public void onClick(View v){
CheckBox cbObject = (CheckBox) v;
int id = cbObject.getId();
if (thumbnailsselection[id]){
cbObject.setChecked(false);
thumbnailsselection[id] = false;
} else {
cbObject.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imageview.setOnClickListener(new OnClickListener(){
public void onClick(View v){
int id = holder.cb.getId();
if (thumbnailsselection[id]){
holder.cb.setChecked(false);
thumbnailsselection[id] = false;
} else{
holder.cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
try{
setBitmap(holder.imageview, ids[position]);
} catch (Throwable e){
log.info("set Bitmap "+ e);
}
holder.cb.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder{
ImageView imageview;
CheckBox cb;
int id;
}
}
  

我关注的这篇文章:

android-gallery-pick-like-whatsapp   MultipleImagePick how-to-select-multiple-images-from-gallery-in-titanium

1 个答案:

答案 0 :(得分:0)

使用以下代码,您可以打开图库浏览

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"),SELECT_IMAGE);