mardi 22 décembre 2015

Capturing, storing and displaying image files in single Activity using multiple onClickListeners and requestCodes

The following code works beautifully for a single Button and ImageView combination in my activity but when I add a second OnClickEventListener and respective code (shown below) to repeat the process for a second Button/ImageView set, the app crashes and will not even open the activity. I've recreated the original layout resource file and am not receiving any errors at compile time. I'm using Android Studio 1.5 and JRE 1.7.0_79. I've researched quite a bit here and have spent too much time already. Any suggestions would be greatly appreciated.

In my .xml file-

        <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp"
            android:paddingBottom="16dp" >

            <Button
                android:id = "@+id/btnSiteDoorPlacard"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Site Door Placard"
                android:onClick="takePhoto1" />

            <ImageView
                android:id="@+id/imgViewSiteDoorPlacard"
                android:layout_width="100dp"
                android:layout_height="150dp"
                android:layout_below="@+id/btnSiteDoorPlacard"
                android:layout_alignRight="@+id/btnSiteDoorPlacard"
                android:layout_alignEnd="@+id/btnSiteDoorPlacard"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Panoramic Picture 1"
                android:id="@+id/btnPanoramicPicture1"
                android:layout_alignBottom="@+id/btnSiteDoorPlacard"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

            <ImageView
                android:id="@+id/imgViewPanoramicPicture1"
                android:layout_width="100dp"
                android:layout_height="150dp"
                android:layout_below="@+id/btnPanoramicPicture1"
                android:layout_alignLeft="@+id/btnPanoramicPicture1"
                android:layout_alignStart="@+id/btnPanoramicPicture1"
                android:layout_alignRight="@+id/btnPanoramicPicture1"
                android:layout_alignEnd="@+id/btnPanoramicPicture1" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Save Before Photos"
                android:id="@+id/btnSaveBeforePhotos"
                android:layout_below="@+id/imgViewSiteDoorPlacard"
                android:layout_centerHorizontal="true" />

        </RelativeLayout>

    </ScrollView>

In my .java file-

    public class PhotoGallery extends Activity {

        static final int TAKE_PHOTO_CODE = 1;
        static final int TAKE_PHOTO2_CODE = 2;

        String filenameSiteDoorPlacard;
        String filenamePanoramicPicture1;
        String filenamePanoramicPicture2;
        String filenamePanoramicPicture3;
        String filenamePanoramicPicture4;

        ImageView imgViewSiteDoorPlacard;
        Button btnSiteDoorPlacard;
        ImageView imgViewPanoramicPicture1;
        Button btnPanaromicPicture1;
        //continue

        SqliteHelper sqliteHelper;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_photo_gallery);

            filenameSiteDoorPlacard = "";
            filenamePanoramicPicture1= "";

            imgViewSiteDoorPlacard = (ImageView) findViewById(R.id.imgViewSiteDoorPlacard);
            btnSiteDoorPlacard = (Button) findViewById(R.id.btnSiteDoorPlacard);
            imgViewPanoramicPicture1 = (ImageView) findViewById(R.id.imgViewPanoramicPicture1);
            btnPanaromicPicture1 = (Button) findViewById(R.id.btnPanaromicPicture1);
            //continue

            btnSiteDoorPlacard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    takePhoto1();
                }
            });

            btnPanaromicPicture1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    takePhoto2();
                }
            });


            sqliteHelper = new SqliteHelper(this);

        }

    //    site door placard
        private void takePhoto1() {
            final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)));
            startActivityForResult(intent, TAKE_PHOTO_CODE);
        }

        private File getTempFile(Context context) {
            //returns /sdcard/Pictures/imgSiteDoorPlacard.jpg
            // String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            final File path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            if (!path.exists()) {
                path.mkdir();
            }
            return new File(path, "SiteDoorPlacard.jpg");
        }
    //      Panoramic Picture1
        private void takePhoto2() {
            final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile2(this)));
            startActivityForResult(intent, TAKE_PHOTO2_CODE);
            }

        private File getTempFile2(Context context) {
            //returns /sdcard/Pictures/imgSiteDoorPlacard.jpg
            // String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            final File path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            if (!path.exists()) {
                path.mkdir();
            }
            return new File(path, "PanaromicPicture1.jpg");
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {
                switch (requestCode) {
                    case TAKE_PHOTO_CODE:
                        final File file = getTempFile(this);
                        // save photo file location to db
                        filenameSiteDoorPlacard = Uri.fromFile(file).toString();
                        boolean result = sqliteHelper.savePhotoGallery(filenameSiteDoorPlacard, filenamePanoramicPicture1, filenamePanoramicPicture2, filenamePanoramicPicture3, filenamePanoramicPicture4);
                        if (result){
                            Toast.makeText(getApplicationContext(), "Photo saved.", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "Failed to save!", Toast.LENGTH_LONG).show();
                        }
                        try {
                            Bitmap captureBmp = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.fromFile(file));
                            // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
                            imgViewSiteDoorPlacard.setImageBitmap(captureBmp);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        break;
                    case TAKE_PHOTO2_CODE:
                        final File file2 = getTempFile2(this);
                        // save photo file location to db
                        filenameSiteDoorPlacard= Uri.fromFile(file2).toString();
                        boolean result2 = sqliteHelper.savePhotoGallery(filenameSiteDoorPlacard, filenamePanoramicPicture1, filenamePanoramicPicture2, filenamePanoramicPicture3, filenamePanoramicPicture4);
                        if (result2){
                            Toast.makeText(getApplicationContext(), "Photo Saved.", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "Failed to save!", Toast.LENGTH_LONG).show();
                        }
                        try {
                            Bitmap captureBmp = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.fromFile(file2));
                            // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
                            imgViewPanoramicPicture1.setImageBitmap(captureBmp);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            }
        }

Aucun commentaire:

Enregistrer un commentaire