Android:从Gallery中获取多个图像,并在android中的列表视图中填充它们

时间:2014-03-10 11:41:05

标签: android listview

我想从Gallery中获取多个图像并在ListView中填充这些图像。首先,是否可以在不使用第三方库的情况下完成它?

这就是我目前所做的...... 我使用universal_image_loader从Gallery获取多个图像,但我无法在ListView中填充它们。

“main.xml” - imageList是包含图库

中图像的列表
<?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:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.85"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/image1"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:layout_weight="1" />

    <ListView
        android:id="@+id/imageList"
        android:layout_width="70dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_weight="1" >

</ListView>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getPhoto"
        android:text="Gallery" />
</LinearLayout>

</LinearLayout>

“list_row.xml” - 列表的单行视图

<?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:orientation="vertical" >

<ImageView
        android:id="@+id/imageThumb"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/index"/>

<TextView 
    android:id="@+id/listText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>


</LinearLayout>

“MainActivity.java”

public class MainActivity extends Activity {

Button buttonPhoto;
ImageView image1;
ListView imageList;
ListAdapter adapter;
ArrayList<String> selectedPhotosList = null;
String[] selectedPhotos = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonPhoto = (Button) findViewById(R.id.button_photo);
    imageList = (ListView) findViewById(R.id.imageList);

    adapter = new ListAdapter(this, selectedPhotosList);
    imageList.setAdapter(adapter);

}

public void getPhoto(View view) {
    Intent i = new Intent(this, MultiPhotoSelectActivity.class);
    startActivityForResult(i, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 0) {
        selectedPhotosList = data.getStringArrayListExtra("selectedPhotos");

        //refresh list
        adapter.notifyDataSetChanged();
        Log.d(MultiPhotoSelectActivity.class.getSimpleName(), "Selected Photos: " + selectedPhotos);
    }
}

}

“ListAdapter.java” - 用于从图库填充图片的列表的适配器

public class ListAdapter extends BaseAdapter {

private static LayoutInflater inflater=null;
private Activity activity;
private ArrayList<String> imageData;

public ListAdapter(Activity a, ArrayList<String> list) {
    activity = a;
    imageData = list;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return imageData.size();
}

@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) {
    View vi = convertView;

    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    ImageView thumb_image=(ImageView)vi.findViewById(R.id.imageThumb); // thumb image
    TextView text=(TextView)vi.findViewById(R.id.listText);

    Bitmap myBitmap = BitmapFactory.decodeFile(imageData.get(position));
    thumb_image.setImageBitmap(myBitmap);
    text.setText(position);

    return vi;
}

}

当我回到MainActivity时,我在ListView中看不到任何图像。请帮帮我。我做错了什么?

修改

“MultiPhotoSelectActivity.java - 返回所有选定图片的路径

package com.example.demo1;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.GridView;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;


public class MultiPhotoSelectActivity extends BaseActivity {

private ArrayList<String> imageUrls;
private DisplayImageOptions options;
private ImageAdapter imageAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ac_image_grid);

    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
    Cursor imagecursor = managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
            null, orderBy + " DESC");

    this.imageUrls = new ArrayList<String>();

    for (int i = 0; i < imagecursor.getCount(); i++) {
        imagecursor.moveToPosition(i);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        imageUrls.add(imagecursor.getString(dataColumnIndex));

        System.out.println("=====> Array path => "+imageUrls.get(i));
    }

    options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.stub_image)
        .showImageForEmptyUri(R.drawable.image_for_empty_url)
        .cacheInMemory()
        .cacheOnDisc()
        .build();

    imageAdapter = new ImageAdapter(this, imageUrls);

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(imageAdapter);
    /*gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startImageGalleryActivity(position);
        }
    });*/
}

@Override
protected void onStop() {
    imageLoader.stop();
    super.onStop();
}

public void btnChoosePhotosClick(View v){

    ArrayList<String> selectedItems = imageAdapter.getCheckedItems();
    Toast.makeText(MultiPhotoSelectActivity.this, "Total photos selected: "+selectedItems.size(), Toast.LENGTH_SHORT).show();
    Log.d(MultiPhotoSelectActivity.class.getSimpleName(), "Selected Items: " + selectedItems.toString());
}

/*private void startImageGalleryActivity(int position) {
    Intent intent = new Intent(this, ImagePagerActivity.class);
    intent.putExtra(Extra.IMAGES, imageUrls);
    intent.putExtra(Extra.IMAGE_POSITION, position);
    startActivity(intent);
}*/

public class ImageAdapter extends BaseAdapter {

    ArrayList<String> mList;
    LayoutInflater mInflater;
    Context mContext;
    SparseBooleanArray mSparseBooleanArray;

    public ImageAdapter(Context context, ArrayList<String> imageList) {
        // TODO Auto-generated constructor stub
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mSparseBooleanArray = new SparseBooleanArray();
        mList = new ArrayList<String>();
        this.mList = imageList;

    }

    public ArrayList<String> getCheckedItems() {
        ArrayList<String> mTempArry = new ArrayList<String>();

        for(int i=0;i<mList.size();i++) {
            if(mSparseBooleanArray.get(i)) {
                mTempArry.add(mList.get(i));
            }
        }

        return mTempArry;
    }

    @Override
    public int getCount() {
        return imageUrls.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.row_multiphoto_item, null);
        }

        CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);

        imageLoader.displayImage("file://"+imageUrls.get(position), imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(Bitmap loadedImage) {
                Animation anim = AnimationUtils.loadAnimation(MultiPhotoSelectActivity.this, R.anim.fade_in);
                imageView.setAnimation(anim);
                anim.start();
            }
        });

        mCheckBox.setTag(position);
        mCheckBox.setChecked(mSparseBooleanArray.get(position));
        mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);

        return convertView;
    }

    OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
        }
    };
}

}

1 个答案:

答案 0 :(得分:0)

您尚未附加MultiPhotoSelectActivity类代码,因此不清楚单击图库按钮后获得的结果。无论如何,您应该使用CursorLoader来查询MediaStore的图像。为什么你自己解码位图而不是使用UIL(就像你提到的那样)或Picasso?