这可能是解决这个问题的最糟糕方式,但我目前仍然坚持并且只是为了这个目标。我目前有一个Activity,我从手机本身的GridView中选择多个图像。我现在正试图在另一个GridView的MainActivity中显示它。我在LinearLayout中看到很多人这样做,但我更喜欢另一个GridView。有一个简单的方法吗?我曾尝试再次显示ImageAdapter,但它为空。
以下是我选择图片的活动:
public class CustomGalleryActivity 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((ListAdapter) imageAdapter);
imagecursor.close();
btnSelect.setOnClickListener(new View.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 View.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 View.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;
}
}
这是我目前将它们显示在LinearLayout中的地方:
@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);
gridView.setAdapter((ListAdapter) imageAdapter);
}
for (int a = 0; a < imagesPath.length; a++)
{
}
}
}
}
答案 0 :(得分:1)
我很久以前就想到了这一点,但我想我应该在这里为其他人提供答案。
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
if(requestCode == PICK_IMAGE_MULTIPLE)
{
if (adapter != null)
{
adapter.clear();
adapter = new ImageAdapter(this, R.id.grdImages, items);
gridView.setAdapter(adapter);
}
imagesPath = data.getStringExtra("data").split("\\|");
for (int i=0; i<imagesPath.length; i++)
{
bitmap = BitmapFactory.decodeFile(imagesPath[i]);
items.add(bitmap);
}
adapter = new ImageAdapter(this, R.id.grdImages, items);
gridView.setAdapter(adapter);
}
}
}