我在学校有这个项目,我必须设计一个应用程序,允许用户提交一份报告,由当局进一步分析。
我希望他能够从相机上传一些图像,以证明他所抱怨的内容是真实的,并且我希望所选图像显示在ADD_REPORT布局内的gridview中。
见下图:
我已经看到了很多不同的实现,但我不太明白何时,何地或为何我应该使用GridViewAdapter或图像适配器,以及如何使图像的数组列表出现在gridview中。
我有以下主类对应图像的布局:
public void ClickAddImagesFromGallery(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
//where do we find the data??
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String pictureDirectoryPath = pictureDirectory.getPath();
//get a Uri representation
Uri data = Uri.parse(pictureDirectoryPath);
//set data and type
photoPickerIntent.setDataAndType(data, "image/*");
//we will invoke this activity and get sth back from it
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, RESULT_LOAD_IMAGE, data);
if (resultCode == RESULT_OK) {
if (requestCode == RESULT_LOAD_IMAGE) {
//universal resource indicator, the image's address
Uri selectedImageUri = data.getData();
//declare a stream to read the image data from the storage environment
try {
InputStream inputStream = getContentResolver().openInputStream(selectedImageUri);
Bitmap imageBitMap = BitmapFactory.decodeStream(inputStream);
//gridView.setAdapter();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Unable to open image", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//de completat
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_report);
spinner = (Spinner) findViewById(R.id.id_spinner_departments);
spinner.setPrompt("Choose one");
spinner.setAdapter(new ArrayAdapter<DepartmentResponsible>(this, R.layout.support_simple_spinner_dropdown_item, DepartmentResponsible.values()));
gridView = (GridView) findViewById(R.id.ImageGridView);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
Here i have the ImageAdapter class:
public class ImageAdapter extends BaseAdapter {
private Context context;
private ArrayList<Image> items;
public ImageAdapter(Context c) {
context = c;
}
//---returns the number of images---
public int getCount() {
return items.size();
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
//imageView.setImageResource();
// obviously here i encounter an error ..
//is this here even correct?
//i am so confused
return imageView;
}
}
Here is the xml containing the Gridview
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/nice_image"
tools:context="ro.ase.pdm.incercare4.AddNewReport">
<TextView
android:id="@+id/id_addReport_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/id_addReport_textview"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.016" />
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:scrollbars="vertical">
<TextView
android:id="@+id/tvTitleReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/TitleTV"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />
<EditText
android:id="@+id/id_EditText_Title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="text"
android:maxLines="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvTitleReport"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<TextView
android:id="@+id/id_textViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DescriptionTV"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_EditText_Title"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
tools:layout_editor_absoluteY="274dp" />
<EditText
android:id="@+id/id_Description_EditText"
android:layout_width="0dp"
android:layout_height="43dp"
android:layout_marginTop="8dp"
android:digits="0123456789"
android:ems="10"
android:inputType="phone"
android:maxLength="10"
android:maxLines="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_textViewDescription"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<TextView
android:id="@+id/id_textViewDepartments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose department"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_Description_EditText"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
tools:layout_editor_absoluteY="274dp" />
<Spinner
android:id="@+id/id_spinner_departments"
style="@style/SpinnerAsEditText"
android:layout_width="250dp"
android:layout_height="50dp"
android:background="@android:drawable/btn_default"
android:prompt="@string/spinner_title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_textViewDepartments"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<GridView
android:id="@+id/ImageGridView"
android:layout_width="wrap_content"
android:layout_height="128dp"
android:layout_weight="1"
android:clickable="true"
android:columnWidth="100dp"
android:contentDescription="@string/desc"
android:drawSelectorOnTop="true"
android:focusable="true"
android:gravity="center"
android:numColumns="3"
android:rowCount="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/id_spinner_departments"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"/>
<Button
android:id="@+id/btn_adaugarePoza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:layout_weight="0"
android:onClick="ClickAddImagesFromGallery"
android:text="Load Picture"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ImageGridView"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<Button
android:id="@+id/btnSubmitReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="fill_horizontal"
android:text="Submit Report"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_adaugarePoza"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
我还有一个包含ImageView的grid_item_layout的xml,但我不确定所有这些是如何运作的,我很难确定为什么我的应用程序在库中选择一个图像而不是在gridview中显示它后会崩溃。
非常感谢您花费此时间!