Example usage for android.media ExifInterface TAG_IMAGE_WIDTH

List of usage examples for android.media ExifInterface TAG_IMAGE_WIDTH

Introduction

In this page you can find the example usage for android.media ExifInterface TAG_IMAGE_WIDTH.

Prototype

String TAG_IMAGE_WIDTH

To view the source code for android.media ExifInterface TAG_IMAGE_WIDTH.

Click Source Link

Document

Type is int.

Usage

From source file:com.angrystone.JpegExifReader.java

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    PluginResult.Status status = PluginResult.Status.OK;
    Integer result = 0;//from   w w  w. jav  a  2  s.  c o m

    if (action.equals("getWidth")) {
        String file = null;
        try {
            file = args.getString(0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            ExifInterface exif = new ExifInterface(file);
            result = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else if (action.equals("getLength")) {
        String file = null;
        try {
            file = args.getString(0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            ExifInterface exif = new ExifInterface(file);
            result = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        status = PluginResult.Status.INVALID_ACTION;
    }
    return new PluginResult(status, result);
}

From source file:Main.java

public static String getImageOrientation(String filepath) {
    if (filepath == null) {
        Log.e(TAG, "GPrintCommon : getImageOrientation() : filepath is null!");
        return unknown;
    }/* w  w w  .j a v a 2  s . c  o m*/

    int imageWidth, imageHeight;

    ExifInterface exif = null;
    try {
        exif = new ExifInterface(filepath);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (exif != null && exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0) > 0) {
        imageWidth = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 1);
        imageHeight = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 1);
    } else {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        Bitmap image = BitmapFactory.decodeFile(filepath, options);
        if (image == null) {
            Log.e(TAG, "GPrintCommon : getImageOrientation() : image is invalid. " + filepath);
            return unknown;
        }

        imageWidth = image.getWidth();
        imageHeight = image.getHeight();
    }

    if (imageWidth > imageHeight) {
        return landscape;
    }

    return portrait;
}

From source file:com.haru.ui.image.workers.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {/*from w  w w  .j  a  v  a  2 s. c o  m*/
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (/* TODO: DEBUG */ true) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (/* TODO: DEBUG */ true) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (/* TODO: DEBUG */ true) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (/* TODO: DEBUG */ true) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}

From source file:com.cars.manager.utils.imageChooser.threads.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {/*ww w.  j ava  2s .  co m*/
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (Config.DEBUG) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (Config.DEBUG) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (Config.DEBUG) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (Config.DEBUG) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}

From source file:Main.java

public static void copyExifData(File srcImgFile, File dstImgFile) throws IOException {
    ExifInterface srcExif = new ExifInterface(srcImgFile.getCanonicalPath());
    ExifInterface dstExif = new ExifInterface(dstImgFile.getCanonicalPath());

    int buildSDKVersion = Build.VERSION.SDK_INT;

    // From API 11
    if (buildSDKVersion >= Build.VERSION_CODES.HONEYCOMB) {
        if (srcExif.getAttribute(ExifInterface.TAG_APERTURE) != null) {
            dstExif.setAttribute(ExifInterface.TAG_APERTURE, srcExif.getAttribute(ExifInterface.TAG_APERTURE));
        }//  w w w  .j  a v a2 s  .c o m
        if (srcExif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME) != null) {
            dstExif.setAttribute(ExifInterface.TAG_EXPOSURE_TIME,
                    srcExif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME));
        }
        if (srcExif.getAttribute(ExifInterface.TAG_ISO) != null) {
            dstExif.setAttribute(ExifInterface.TAG_ISO, srcExif.getAttribute(ExifInterface.TAG_ISO));
        }
    }

    // From API 9
    if (buildSDKVersion >= Build.VERSION_CODES.GINGERBREAD) {
        if (srcExif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE) != null) {
            dstExif.setAttribute(ExifInterface.TAG_GPS_ALTITUDE,
                    srcExif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE));
        }
        if (srcExif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF) != null) {
            dstExif.setAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF,
                    srcExif.getAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF));
        }
    }

    // From API 8
    if (buildSDKVersion >= Build.VERSION_CODES.FROYO) {
        if (srcExif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH) != null) {
            dstExif.setAttribute(ExifInterface.TAG_FOCAL_LENGTH,
                    srcExif.getAttribute(ExifInterface.TAG_FOCAL_LENGTH));
        }
        if (srcExif.getAttribute(ExifInterface.TAG_GPS_DATESTAMP) != null) {
            dstExif.setAttribute(ExifInterface.TAG_GPS_DATESTAMP,
                    srcExif.getAttribute(ExifInterface.TAG_GPS_DATESTAMP));
        }
        if (srcExif.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD) != null) {
            dstExif.setAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD,
                    srcExif.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD));
        }
        if (srcExif.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP) != null) {
            dstExif.setAttribute(ExifInterface.TAG_GPS_TIMESTAMP,
                    srcExif.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP));
        }
    }

    if (srcExif.getAttribute(ExifInterface.TAG_DATETIME) != null) {
        dstExif.setAttribute(ExifInterface.TAG_DATETIME, srcExif.getAttribute(ExifInterface.TAG_DATETIME));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_FLASH) != null) {
        dstExif.setAttribute(ExifInterface.TAG_FLASH, srcExif.getAttribute(ExifInterface.TAG_FLASH));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) != null) {
        dstExif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,
                srcExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF) != null) {
        dstExif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF,
                srcExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE) != null) {
        dstExif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,
                srcExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF) != null) {
        dstExif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF,
                srcExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH) != null) {
        dstExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH,
                srcExif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH) != null) {
        dstExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH,
                srcExif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_MAKE) != null) {
        dstExif.setAttribute(ExifInterface.TAG_MAKE, srcExif.getAttribute(ExifInterface.TAG_MAKE));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_MODEL) != null) {
        dstExif.setAttribute(ExifInterface.TAG_MODEL, srcExif.getAttribute(ExifInterface.TAG_MODEL));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_ORIENTATION) != null) {
        dstExif.setAttribute(ExifInterface.TAG_ORIENTATION,
                srcExif.getAttribute(ExifInterface.TAG_ORIENTATION));
    }
    if (srcExif.getAttribute(ExifInterface.TAG_WHITE_BALANCE) != null) {
        dstExif.setAttribute(ExifInterface.TAG_WHITE_BALANCE,
                srcExif.getAttribute(ExifInterface.TAG_WHITE_BALANCE));
    }

    dstExif.saveAttributes();
}

From source file:com.wots.lutmaar.CustomView.imagechooser.threads.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {/*from w  ww  .  j  a  va  2s .c om*/
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);

        }
        /* if (scale == 1)
        bitmap = Bitmap.createScaledBitmap(bitmap, 240, 260, true);*/
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (BuildConfig.DEBUG) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (BuildConfig.DEBUG) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}

From source file:com.amytech.android.library.views.imagechooser.threads.MediaProcessorThread.java

private String compressAndSaveImage(String fileImage, int scale) throws Exception {
    try {/*  www.j  a  va 2 s  . c  o  m*/
        ExifInterface exif = new ExifInterface(fileImage);
        String width = exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
        String length = exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotate = 0;
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Before: " + width + "x" + length);
        }

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = -90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        int w = Integer.parseInt(width);
        int l = Integer.parseInt(length);

        int what = w > l ? w : l;

        Options options = new Options();
        if (what > 1500) {
            options.inSampleSize = scale * 4;
        } else if (what > 1000 && what <= 1500) {
            options.inSampleSize = scale * 3;
        } else if (what > 400 && what <= 1000) {
            options.inSampleSize = scale * 2;
        } else {
            options.inSampleSize = scale;
        }
        if (BuildConfig.DEBUG) {
            Log.i(TAG, "Scale: " + (what / options.inSampleSize));
            Log.i(TAG, "Rotate: " + rotate);
        }
        Bitmap bitmap = BitmapFactory.decodeFile(fileImage, options);
        File original = new File(fileImage);
        File file = new File((original.getParent() + File.separator
                + original.getName().replace(".", "_fact_" + scale + ".")));
        FileOutputStream stream = new FileOutputStream(file);
        if (rotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(rotate);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        }
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        if (BuildConfig.DEBUG) {
            ExifInterface exifAfter = new ExifInterface(file.getAbsolutePath());
            String widthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
            String lengthAfter = exifAfter.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
            if (BuildConfig.DEBUG) {
                Log.i(TAG, "After: " + widthAfter + "x" + lengthAfter);
            }
        }
        stream.flush();
        stream.close();
        return file.getAbsolutePath();

    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Corrupt or deleted file???");
    }
}

From source file:com.nextgis.ngm_clink_monitoring.fragments.ObjectStatusFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    File tempPhotoFile = new File(mTempPhotoPath);

    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
        GISApplication app = (GISApplication) getActivity().getApplication();
        ContentResolver contentResolver = app.getContentResolver();
        String photoFileName = getPhotoFileName();

        try {// ww w. j  a va  2s .  c om
            BitmapUtil.writeLocationToExif(tempPhotoFile, app.getCurrentLocation(), 0);
        } catch (IOException e) {
            Log.d(TAG, e.getLocalizedMessage());
        }

        Uri allAttachesUri = Uri.parse("content://" + FoclSettingsConstantsUI.AUTHORITY + "/" + mObjectLayerName
                + "/" + mObjectId + "/attach");

        ContentValues values = new ContentValues();
        values.put(VectorLayer.ATTACH_DISPLAY_NAME, photoFileName);
        values.put(VectorLayer.ATTACH_MIME_TYPE, "image/jpeg");
        //values.put(VectorLayer.ATTACH_DESCRIPTION, photoFileName);

        Uri attachUri = null;
        try {
            attachUri = contentResolver.insert(allAttachesUri, values);

        } catch (Exception e) {
            Log.d(TAG, e.getLocalizedMessage());
        }

        if (null != attachUri) {

            try {
                int exifOrientation = BitmapUtil.getOrientationFromExif(tempPhotoFile);

                // resize and rotate
                Bitmap sourceBitmap = BitmapFactory.decodeFile(tempPhotoFile.getPath());
                Bitmap resizedBitmap = BitmapUtil.getResizedBitmap(sourceBitmap,
                        FoclConstants.PHOTO_MAX_SIZE_PX, FoclConstants.PHOTO_MAX_SIZE_PX);
                Bitmap rotatedBitmap = BitmapUtil.rotateBitmap(resizedBitmap, exifOrientation);

                // jpeg compress
                File tempAttachFile = File.createTempFile("attach", null, app.getCacheDir());
                OutputStream tempOutStream = new FileOutputStream(tempAttachFile);
                rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, FoclConstants.PHOTO_JPEG_COMPRESS_QUALITY,
                        tempOutStream);
                tempOutStream.close();

                int newHeight = rotatedBitmap.getHeight();
                int newWidth = rotatedBitmap.getWidth();

                rotatedBitmap.recycle();

                // write EXIF to new file
                BitmapUtil.copyExifData(tempPhotoFile, tempAttachFile);

                ExifInterface attachExif = new ExifInterface(tempAttachFile.getCanonicalPath());

                attachExif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + ExifInterface.ORIENTATION_NORMAL);
                attachExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, "" + newHeight);
                attachExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, "" + newWidth);

                attachExif.saveAttributes();

                // attach data from tempAttachFile
                OutputStream attachOutStream = contentResolver.openOutputStream(attachUri);
                FoclFileUtil.copy(new FileInputStream(tempAttachFile), attachOutStream);
                attachOutStream.close();

                tempAttachFile.delete();

            } catch (IOException e) {
                Toast.makeText(getActivity(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            }

            Log.d(TAG, attachUri.toString());

        } else {
            Log.d(TAG, "insert attach failed");
        }

        try {
            if (app.isOriginalPhotoSaving()) {
                File origPhotoFile = new File(getDailyPhotoFolder(), photoFileName);

                if (!com.nextgis.maplib.util.FileUtil.move(tempPhotoFile, origPhotoFile)) {
                    Toast.makeText(getActivity(), "Save original photo failed", Toast.LENGTH_LONG).show();
                }

            } else {
                tempPhotoFile.delete();
            }

            setPhotoGalleryAdapter();
            setPhotoGalleryVisibility(true);

        } catch (IOException e) {
            Toast.makeText(getActivity(), e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    }

    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == Activity.RESULT_CANCELED) {
        tempPhotoFile.delete();
    }
}

From source file:com.nextgis.ngm_clink_monitoring.fragments.CreateObjectFragment.java

protected void writePhotoAttach(File tempPhotoFile) throws IOException {
    GISApplication app = (GISApplication) getActivity().getApplication();

    ContentResolver contentResolver = app.getContentResolver();
    String photoFileName = getPhotoFileName(tempPhotoFile);
    Log.d(TAG, "CreateObjectFragment, writePhotoAttach(), photoFileName: " + photoFileName);

    Uri allAttachesUri = Uri.parse("content://" + FoclSettingsConstantsUI.AUTHORITY + "/" + mObjectLayerName
            + "/" + mObjectId + "/attach");
    Log.d(TAG, "CreateObjectFragment, writePhotoAttach(), allAttachesUri: " + allAttachesUri);

    ContentValues values = new ContentValues();
    values.put(VectorLayer.ATTACH_DISPLAY_NAME, photoFileName);
    values.put(VectorLayer.ATTACH_MIME_TYPE, "image/jpeg");
    //values.put(VectorLayer.ATTACH_DESCRIPTION, photoFileName);

    Uri attachUri = null;// w  w w .j a  v  a 2s  .c o m
    String insertAttachError = null;
    try {
        attachUri = contentResolver.insert(allAttachesUri, values);
        Log.d(TAG, "CreateObjectFragment, writePhotoAttach(), insert: " + attachUri.toString());
    } catch (Exception e) {
        Log.d(TAG,
                "CreateObjectFragment, writePhotoAttach(), Insert attach failed: " + e.getLocalizedMessage());
        insertAttachError = "Insert attach failed: " + e.getLocalizedMessage();
    }

    if (null != attachUri) {
        int exifOrientation = BitmapUtil.getOrientationFromExif(tempPhotoFile);

        // resize and rotate
        Bitmap sourceBitmap = BitmapFactory.decodeFile(tempPhotoFile.getPath());
        Bitmap resizedBitmap = BitmapUtil.getResizedBitmap(sourceBitmap, FoclConstants.PHOTO_MAX_SIZE_PX,
                FoclConstants.PHOTO_MAX_SIZE_PX);
        Bitmap rotatedBitmap = BitmapUtil.rotateBitmap(resizedBitmap, exifOrientation);

        // jpeg compress
        File tempAttachFile = File.createTempFile("attach", null, app.getCacheDir());
        OutputStream tempOutStream = new FileOutputStream(tempAttachFile);
        rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, FoclConstants.PHOTO_JPEG_COMPRESS_QUALITY,
                tempOutStream);
        tempOutStream.close();

        int newHeight = rotatedBitmap.getHeight();
        int newWidth = rotatedBitmap.getWidth();

        rotatedBitmap.recycle();

        // write EXIF to new file
        BitmapUtil.copyExifData(tempPhotoFile, tempAttachFile);
        BitmapUtil.writeLocationToExif(tempAttachFile, mAccurateLocation, app.getGpsTimeOffset());

        ExifInterface attachExif = new ExifInterface(tempAttachFile.getCanonicalPath());

        attachExif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + ExifInterface.ORIENTATION_NORMAL);
        attachExif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, "" + newHeight);
        attachExif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, "" + newWidth);

        attachExif.saveAttributes();

        // attach data from tempAttachFile
        OutputStream attachOutStream = contentResolver.openOutputStream(attachUri);
        if (attachOutStream != null) {
            FoclFileUtil.copy(new FileInputStream(tempAttachFile), attachOutStream);
            attachOutStream.close();
        } else {
            Log.d(TAG, "CreateObjectFragment, writePhotoAttach(), attachOutStream == null, attachUri"
                    + attachUri.toString());
        }

        if (!tempAttachFile.delete()) {
            Log.d(TAG,
                    "CreateObjectFragment, writePhotoAttach(), tempAttachFile.delete() failed, tempAttachFile:"
                            + tempAttachFile.getAbsolutePath());
        }
    }

    if (app.isOriginalPhotoSaving()) {
        BitmapUtil.writeLocationToExif(tempPhotoFile, mAccurateLocation, app.getGpsTimeOffset());
        File origPhotoFile = new File(getDailyPhotoFolder(), photoFileName);

        if (!com.nextgis.maplib.util.FileUtil.move(tempPhotoFile, origPhotoFile)) {
            Log.d(TAG, "CreateObjectFragment, writePhotoAttach(), move original failed, tempPhotoFile:"
                    + tempPhotoFile.getAbsolutePath() + ", origPhotoFile: " + origPhotoFile.getAbsolutePath());
            throw new IOException(
                    "Save original photo failed, tempPhotoFile: " + tempPhotoFile.getAbsolutePath());
        }

    } else {
        if (!tempPhotoFile.delete()) {
            Log.d(TAG, "CreateObjectFragment, writePhotoAttach(), tempPhotoFile.delete() failed, tempPhotoFile:"
                    + tempPhotoFile.getAbsolutePath());
        }
    }

    if (null != insertAttachError) {
        throw new IOException(insertAttachError);
    }
}