gpt4 book ai didi

java - 如何使用android中照片库 GridView 中的复选框获取选中图片的路径

转载 作者:行者123 更新时间:2023-12-01 15:48:48 25 4
gpt4 key购买 nike

我正在尝试从 GridView 中的图库获取照片的路径。该画廊由每个缩略图以及附加的复选框组成。完整代码如下:

    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>

请帮助我。提前致谢

最佳答案

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








关于java - 如何使用android中照片库 GridView 中的复选框获取选中图片的路径,我们在Stack Overflow上找到一个类似的问题:

https://stackoverflow.com/questions/6547746/




25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com