0

我正在尝试从网格视图中的图库中获取照片路径。这个画廊由带有附加复选框的每个缩略图组成。这是整个代码:

    public class GridGallery extends Activity
{

    ArrayList<String>list;
AlertDialog.Builder alert;
private Button send;
GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.grid_gallery);
    DataModel dbModel = new DataModel(this);
    list = dbModel.selectAll();               

    alert = new AlertDialog.Builder(GridGallery.this);
    send = (Button)findViewById(R.id.send_message);
    gridView = (GridView) findViewById(R.id.sdcard);
    gridView.setAdapter(new ImageAdapter(this));

            gridView.setClickable(true);        
    gridView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View view, int pos,
                long id) 
        {
            // TODO Auto-generated method stub
            final int position = pos;
            final String path = list.get(position).toString();
            final String option[] = new String[]{"Send to","Watch"};
            alert.setTitle("Pick options");
            alert.setItems(option, new  OnClickListener() {

                public void onClick(DialogInterface dialog, int which) 
                {
                    // TODO Auto-generated method stub
                    if(option[which].equals("Watch"))
                    {
                        if(path.contains(".jpg"))
                        {
                            Intent intent = new Intent(Intent.ACTION_VIEW);        
                            intent.setDataAndType(Uri.fromFile(new File(list.get(position))), "image/jpeg");
                            startActivity(intent);                  
                        }
                        else if(path.contains(".mp4"))
                        {
                            Intent intent = new Intent(Intent.ACTION_VIEW);        
                            intent.setDataAndType(Uri.fromFile(new File(list.get(position))), "video/*");
                            startActivity(intent);
                        }
                        else
                        {
                            Intent intent = new Intent(Intent.ACTION_VIEW);        
                            intent.setDataAndType(Uri.fromFile(new File(list.get(position))), "audio/*");
                            startActivity(intent);
                        }
                    }//
                    else
                    {
                        Intent sendMail = new Intent(GridGallery.this, SendMessage.class);
                        sendMail.putExtra("path", path);
                        startActivity(sendMail);                            
                    }
                }
            }).show();
        }
    });
    send.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            String path = null;             

            Intent sendToMail = new Intent(GridGallery.this, SendMessage.class);
            sendToMail.putExtra("path", path);
            startActivity(sendToMail);

        }
    });
}


 /**
 * Adapter for our image files.
 */
private class ImageAdapter extends BaseAdapter 
{

    private final Context context; 
    Bitmap bitmap;

    public ImageAdapter(Context localContext) {
        context = localContext;
    }

    public int getCount() 
    {
        return list.size();
    }
    public Object getItem(int position) 
    {
        return position;
    }
    public long getItemId(int position) 
    {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView picturesView;
        View myView = convertView; 
        if (convertView == null) 
        {

            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//getLayoutInflater();
            myView = layoutInflater.inflate(R.layout.image_selection, null);

            picturesView = new ImageView(context);
            picturesView = (ImageView)myView.findViewById(R.id.item_grid);
            picturesView.setClickable(true);

            if(list.get(position).contains(".jpg"))
            {
                 bitmap = BitmapFactory.decodeFile(list.get(position)); 
            }
            else if(list.get(position).contains(".mp4"))
            {
                bitmap = ThumbnailUtils.createVideoThumbnail(list.get(position), 0); 
            }
            else
            {

            }

            picturesView.setImageBitmap(bitmap);
            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(8, 8, 8, 8);
            return myView;
        }
        else 
        {
            myView = convertView;
            return myView;
        }

    }
}

}

我的问题是我无法单击图像或视频缩略图。还有当我选中复选框时如何获取图像。

这是 Image_selection 的 XML 代码:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" 
          android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal">
         <ImageView  android:id="@+id/item_grid" android:layout_width="100dip" android:layout_height="100dip"/>
         <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" />

和 grid_gallery.xml:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/sdcard"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>
</RelativeLayout>

请帮我。提前致谢

4

1 回答 1

1

看来您将图像路径存储在 ArrayList 列表中。如果是这样,则为 GridView 设置一个onItemClickListeber。在该onItemClick方法中,您可以获得被点击的 gridview 的位置。尝试 'list.get(position) in theonItemClick` 来获取路径

于 2011-07-01T12:36:25.683 回答