lundi 27 juin 2016

Implementing a Gallery within an Android app

I am currently working on a small app and have the intention of building a gallery within the app. I currently have the camera intent working but I am not sure how to display the stored images in a grid (i.e. GridView). Here is the 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;
    }

I am wondering if storing the images that are returned by createImageFile() in a list and displaying the items from the list in a gridView fashion is feasible. Here is the .xml for the Gallery:

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

As you can see in the xml, I have the button for the camera and the GridView in the same file. Would it be better to have them separated? and finally, any implementation suggestions for my intentions (i.e. taking a photo, storing it, then displaying it in the grid)?

Aucun commentaire:

Enregistrer un commentaire