How to get path of checked picture using checkbox from grid view of photo Gallery in android

主宰稳场 提交于 2019-12-12 01:40:52

问题


I am trying to get path of photo from gallery which in grid view. this gallery consists of each thumbnail with attached checkbox. Here is the whole code:

    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;
        }

    }
}

}

MY problem is I can not be able to click the image or video thumbnail. also how do I able to get the image when I checked the check box.

here is XML code for Image_selection:-

<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" />

and 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>

please help me. Thanks in advance


回答1:


it seems you store the image path in the ArrayList list. If so, then set an onItemClickListeber for the GridView. in the onItemClick method you get the position of the gridview which was clicked. try 'list.get(position)in theonItemClick` to get the path



来源:https://stackoverflow.com/questions/6547746/how-to-get-path-of-checked-picture-using-checkbox-from-grid-view-of-photo-galler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!