我目前正在开发一款小应用,并打算在应用内构建一个图库。我目前有相机意图工作,但我不知道如何在网格中显示存储的图像(即GridView)。这是GalleryActivity:
public class GalleryActivity extends Activity {
TextView header;
private static final int ACT_START_CAMERA_APP = 0; //indicator to start the camera app
private ImageView capturedPhoto;
private String imageFileLocation = " ";
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.gallery_activity);
capturedPhoto = (ImageView) findViewById(R.id.viewCapturedPhoto);
header = (TextView) findViewById(R.id.heading);
header.setTextKeepState("My Gallery");
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
//inflate the menu; adds items to the action bar if present
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
//handles action bar item click here. The action bar
//will automatically handle clicks on the home button
//as long as you specify a parent activity in AndroidManifest.xml
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if (id == R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
}
//invoked ACTION_IMAGE_CAPTURE - native Android method to access the camera
public void TakePhoto(View view){
Intent callCameraApplicationIntent = new Intent();
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try{
photoFile = createImageFile(); //will contain the address of the captured photo
} catch (IOException exception) {
exception.getStackTrace();
}
//intent used to store the requested image/file i.e. photoFile
//tells Android where to store the picture
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(callCameraApplicationIntent, ACT_START_CAMERA_APP);
}
//if we successfully take a picture print "Picture saved"
//else return "Cannot connect to camera" by default
protected void onActivityResult (int requestCode, int resultCode, Intent data){
//check if you have similar results for defining ACT_START_CAMERA_APP and resultCode
//if so, continue
if(requestCode == ACT_START_CAMERA_APP && resultCode == RESULT_OK) {
Toast.makeText(this, "Picture saved", Toast.LENGTH_SHORT).show();
Bitmap capturedPhotoBitmap = BitmapFactory.decodeFile(imageFileLocation);
capturedPhoto.setImageBitmap(capturedPhotoBitmap);
}
}
//allows storage of pictures on the phone
//create non-collision fileNames i.e. that do not have the same name and appends a stamp
//to the photo to make it unique
File createImageFile() throws IOException {
//create timestamp
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
//append timestamp to generic name IMAGE_
String imageFileName = "IMAGE_" + timeStamp + "_";
File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//now create the file
File image = File.createTempFile(imageFileName, ".jpg", storageDirectory);
imageFileLocation = image.getAbsolutePath();
return image;
}
我想知道是否将createImageFile()
返回的图像存储在列表中并以gridView方式显示列表中的项目是可行的。这是Gallery的.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="My Gallery"
android:textSize="30dp"
android:gravity="center"
android:textStyle="bold"
android:clickable="true"
android:background="@android:color/holo_blue_light"
android:textColor="#FFFFFF"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera"
android:id="@+id/photoButton"
android:onClick="TakePhoto"
android:clickable="true"
android:enabled="true"
android:layout_alignBottom="@+id/heading"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/photoButton"
android:id="@+id/viewCapturedPhoto"
android:clickable="true"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/heading">
</GridView>
</RelativeLayout>
正如您在xml中看到的那样,我在同一个文件中有相机按钮和GridView。将它们分开会更好吗?最后,我的意图的任何实施建议(即拍照,存储,然后在网格中显示)?