Package com.cheetahdigital.uikit.helper
Class MediaFileHelper
- java.lang.Object
-
- com.cheetahdigital.uikit.helper.MediaFileHelper
-
public class MediaFileHelper extends java.lang.Object
Helper class for handling compression and transformation of media files.
-
-
Field Summary
Fields Modifier and Type Field Description static int
OPTIONS_RECYCLE_INPUT
Constant used to indicate we should recycle the input inextractThumbnail(Bitmap, int, int, int)
unless the output is the input.static int
TARGET_SIZE_MICRO_THUMBNAIL
Constant used to indicate the dimension of micro thumbnail.static int
TARGET_SIZE_MINI_THUMBNAIL
Constant used to indicate the dimension of mini thumbnail.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
calculateInSampleSize(android.graphics.BitmapFactory.Options options, int reqWidth, int reqHeight)
From Android's sample here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.htmlstatic MediaFile
compressImageIntoMediaFile(android.content.Context appContext, android.graphics.Bitmap imageBitmap)
Compress and resize an image if needed, then converts into aMediaFile
ready to be uploaded to the server.static MediaFile
compressImageIntoMediaFile(android.content.Context appContext, android.graphics.Bitmap imageBitmap, int requestedDimension)
Compress and resize an image if needed, then converts into aMediaFile
ready to be uploaded to the server.static MediaFile
compressImageIntoMediaFile(android.content.Context appContext, java.io.File imageFile)
Compress and resize an image if needed, then converts into aMediaFile
ready to be uploaded to the server.static MediaFile
compressImageIntoMediaFile(android.content.Context appContext, java.io.File imageFile, int requestedDimension)
Compress and resize an image if needed, then converts into aMediaFile
ready to be uploaded to the server.static java.util.List<MediaFile>
compressMediaFiles(android.content.Context appContext, java.io.File[] rawFiles)
Compress a list of Supported Media files.static java.util.List<byte[]>
compressMediaFiles(java.io.File[] rawFiles)
static MediaFile
compressVideoIntoMediaFile(java.io.File videoFile, java.lang.String outPath)
Compress the video file into a size similar to iOS SDK's AVAssetExportPreset960x540, then convert into aMediaFile
which will be saved on the given outPath and ready to be uploaded to the server.static java.io.File
createFileFromBitmap(android.app.Activity activity, android.graphics.Bitmap source)
Creates a temporary image file in app's cachestatic android.graphics.Bitmap
createImageThumbnail(java.lang.String filePath, int kind)
This method first examines if the thumbnail embedded in EXIF is bigger than our target size.static java.io.File
createTempImageFile(android.content.Context context, android.graphics.Bitmap image)
Create Temporary Image File with filename accdg to date createdstatic java.lang.String
downloadImage(android.content.Context context, android.net.Uri selectedImage)
Download and save an imagestatic android.graphics.Bitmap
downsizeBitmap(android.graphics.Bitmap imageBitmap)
Reduce bitmap sizestatic android.graphics.Bitmap
downsizeBitmap(android.graphics.Bitmap imageBitmap, int reqWidth, int reqHeight)
Downsize an image to its required width and height.static android.graphics.Bitmap
extractThumbnail(android.graphics.Bitmap source, int width, int height)
Creates a centered bitmap of the desired size.static android.graphics.Bitmap
extractThumbnail(android.graphics.Bitmap source, int width, int height, int options)
Creates a centered bitmap of the desired size.static java.lang.String
getDataColumn(android.content.Context context, android.net.Uri uri, java.lang.String selection, java.lang.String[] selectionArgs)
Get the value of the data column for this Uri.static java.lang.String
getFileNameWithoutExtension(java.lang.String fileNameWithExtension)
Gets the absolute Filename without the extension.static java.lang.String
getFilePathForN(android.content.Context context, android.net.Uri uri)
Find file path for N or higher OS veersionstatic java.lang.String
getImageUri(android.content.Context context, android.graphics.Bitmap inImage)
Find the file path of theBitmap
static java.lang.String
getPath(android.content.Context context, android.net.Uri uri)
Get a file path from a Uri.static java.lang.String
getPathFromMediaStore(android.app.Activity activity, android.net.Uri uri)
Get a file path from a Uri from the MediaStore Datastatic android.graphics.Bitmap
getThumbnail(android.content.Context context, android.net.Uri uri)
Returns the thumbnail image from a given uri.static boolean
isDownloadsDocument(android.net.Uri uri)
static boolean
isExternalStorageDocument(android.net.Uri uri)
static boolean
isFileSizeExceed(MediaFile... files)
Check files if exceeding server requirementsstatic boolean
isFileSizeExceed(java.util.List<MediaFile> files)
Check files if exceeding server requirementsstatic boolean
isMediaDocument(android.net.Uri uri)
static boolean
isSupportedImageFile(java.lang.String filePath)
Supported Image File Extensions: png jpeg jpg gifstatic boolean
isSupportedVideoFile(java.lang.String filePath)
Supported Video File Formats tested through Android CTS http://developer.android.com/guide/appendix/media-formats.html 3gp mp4 ts mkv webmstatic java.io.File[]
parseMediaUris(java.lang.String[] uriStrings)
Parses array of string urisstatic android.graphics.Bitmap
rotateBitmap(android.graphics.Bitmap bitmap, int orientation)
Rotate the bitmap with the specified orientation.static android.graphics.Bitmap
rotateImage(android.graphics.Bitmap img, int degree)
Rotates an image to a specified degree.static android.graphics.Bitmap
rotateImageIfRequired(android.graphics.Bitmap img, android.net.Uri selectedImage)
Rotate an image if required.static java.io.File
saveBitmap(android.content.Context context, android.graphics.Bitmap bitmap, java.lang.String fileName, int compressQuality)
Saves bitmap in the files directorystatic java.io.File
saveBitmapToFile(android.graphics.Bitmap bitmap)
Save the image in jpg format.
-
-
-
Field Detail
-
OPTIONS_RECYCLE_INPUT
public static final int OPTIONS_RECYCLE_INPUT
Constant used to indicate we should recycle the input inextractThumbnail(Bitmap, int, int, int)
unless the output is the input.- See Also:
- Constant Field Values
-
TARGET_SIZE_MINI_THUMBNAIL
public static final int TARGET_SIZE_MINI_THUMBNAIL
Constant used to indicate the dimension of mini thumbnail.Only used by media framework and media provider internally.
- See Also:
- Constant Field Values
-
TARGET_SIZE_MICRO_THUMBNAIL
public static final int TARGET_SIZE_MICRO_THUMBNAIL
Constant used to indicate the dimension of micro thumbnail.Only used by media framework and media provider internally.
- See Also:
- Constant Field Values
-
-
Method Detail
-
getPath
public static java.lang.String getPath(android.content.Context context, android.net.Uri uri)
Get a file path from a Uri. This will get the path for Storage Access Framework Documents, as well as the _data field for the MediaStore and other file-based ContentProviders.- Parameters:
context
- The context.uri
- The Uri to query.- Returns:
- file path from the
Uri
-
getPathFromMediaStore
public static java.lang.String getPathFromMediaStore(android.app.Activity activity, android.net.Uri uri)
Get a file path from a Uri from the MediaStore Data- Parameters:
activity
- callingActivity
uri
-Uri
path- Returns:
- String path of the Media
-
saveBitmapToFile
public static java.io.File saveBitmapToFile(android.graphics.Bitmap bitmap)
Save the image in jpg format. This is save in the pictures/Attachments folder- Parameters:
bitmap
- bitmap file of the image- Returns:
- File of the bitmap saved
-
getDataColumn
public static java.lang.String getDataColumn(android.content.Context context, android.net.Uri uri, java.lang.String selection, java.lang.String[] selectionArgs)
Get the value of the data column for this Uri. This is useful for MediaStore Uris, and other file-based ContentProviders.- Parameters:
context
- The context.uri
- The Uri to query.selection
- (Optional) Filter used in the query.selectionArgs
- (Optional) Selection arguments used in the query.- Returns:
- The value of the _data column, which is typically a file path.
-
isExternalStorageDocument
public static boolean isExternalStorageDocument(android.net.Uri uri)
- Parameters:
uri
- The Uri to check.- Returns:
- Whether the Uri authority is ExternalStorageProvider.
-
isDownloadsDocument
public static boolean isDownloadsDocument(android.net.Uri uri)
- Parameters:
uri
- The Uri to check.- Returns:
- Whether the Uri authority is DownloadsProvider.
-
isMediaDocument
public static boolean isMediaDocument(android.net.Uri uri)
- Parameters:
uri
- The Uri to check.- Returns:
- Whether the Uri authority is MediaProvider.
-
isFileSizeExceed
public static boolean isFileSizeExceed(java.util.List<MediaFile> files)
Check files if exceeding server requirements- Parameters:
files
- List of files for upload- Returns:
- true if total size does not exceed limit, else false
-
isFileSizeExceed
public static boolean isFileSizeExceed(MediaFile... files)
Check files if exceeding server requirements- Parameters:
files
- List of files for upload- Returns:
- true if total size does not exceed limit, else false
-
compressMediaFiles
public static java.util.List<byte[]> compressMediaFiles(java.io.File[] rawFiles)
-
compressMediaFiles
@WorkerThread public static java.util.List<MediaFile> compressMediaFiles(android.content.Context appContext, java.io.File[] rawFiles)
Compress a list of Supported Media files. Supported Image Formats:- png
- jpeg
- jpg
- gif
- 3gp
- mp4
- ts
- mkv
- webm
- Parameters:
appContext
- used for getting resourcesrawFiles
- image or video files that are supported.- Returns:
- the compressed list of images and videos.
-
isSupportedImageFile
public static boolean isSupportedImageFile(java.lang.String filePath)
Supported Image File Extensions:- png
- jpeg
- jpg
- gif
- Parameters:
filePath
- path of file to be checked- Returns:
- true if file is supported, else false
-
isSupportedVideoFile
public static boolean isSupportedVideoFile(java.lang.String filePath)
Supported Video File Formats tested through Android CTS http://developer.android.com/guide/appendix/media-formats.html- 3gp
- mp4
- ts
- mkv
- webm
- Parameters:
filePath
- path of file to be checked- Returns:
- true if file is supported, else false
-
compressImageIntoMediaFile
public static MediaFile compressImageIntoMediaFile(android.content.Context appContext, java.io.File imageFile)
Compress and resize an image if needed, then converts into aMediaFile
ready to be uploaded to the server.- Parameters:
appContext
- used to get resourcesimageFile
- must be an image file or else an exception will occur.- Returns:
- converted file to
MediaFile
-
compressImageIntoMediaFile
public static MediaFile compressImageIntoMediaFile(android.content.Context appContext, android.graphics.Bitmap imageBitmap)
Compress and resize an image if needed, then converts into aMediaFile
ready to be uploaded to the server.- Parameters:
appContext
- used to get resourcesimageBitmap
-Bitmap
file to be converted- Returns:
- converted file to
MediaFile
-
compressImageIntoMediaFile
public static MediaFile compressImageIntoMediaFile(android.content.Context appContext, java.io.File imageFile, int requestedDimension)
Compress and resize an image if needed, then converts into aMediaFile
ready to be uploaded to the server.- Parameters:
appContext
- used to get resourcesimageFile
- must be an image file or else an exception will occur.requestedDimension
- image will be scaled until the requested value is achieved.- Returns:
- converted file to
MediaFile
-
compressImageIntoMediaFile
public static MediaFile compressImageIntoMediaFile(android.content.Context appContext, android.graphics.Bitmap imageBitmap, int requestedDimension)
Compress and resize an image if needed, then converts into aMediaFile
ready to be uploaded to the server.- Parameters:
appContext
- used to get resourcesimageBitmap
- must beBitmap
or else an exception will occur.requestedDimension
- image will be scaled until the requested value is achieved.- Returns:
- converted file to
MediaFile
-
compressVideoIntoMediaFile
@WorkerThread public static MediaFile compressVideoIntoMediaFile(java.io.File videoFile, java.lang.String outPath)
Compress the video file into a size similar to iOS SDK's AVAssetExportPreset960x540, then convert into aMediaFile
which will be saved on the given outPath and ready to be uploaded to the server. If the resolution is lower than 960x540, no compression will be done.NOTE: Compression will only work for API version >= 18 (Jellybean MR2) due to
MediaMuxer
, for older versions, the original video file will be returned.- Parameters:
videoFile
- must be a video file that is supported by AndroidoutPath
- the path for the output- Returns:
- converted file to
MediaFile
-
downsizeBitmap
public static android.graphics.Bitmap downsizeBitmap(android.graphics.Bitmap imageBitmap, int reqWidth, int reqHeight)
Downsize an image to its required width and height.- Parameters:
imageBitmap
-Bitmap
to be processedreqWidth
- widht configurationreqHeight
- height configuration- Returns:
- processed
Bitmap
-
downsizeBitmap
public static android.graphics.Bitmap downsizeBitmap(android.graphics.Bitmap imageBitmap)
Reduce bitmap size- Parameters:
imageBitmap
- sourceBitmap
to be processed- Returns:
- reduced
Bitmap
-
calculateInSampleSize
public static int calculateInSampleSize(android.graphics.BitmapFactory.Options options, int reqWidth, int reqHeight)
From Android's sample here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html- Parameters:
options
-BitmapFactory.Options
used for configurationreqWidth
- width configurationreqHeight
- height configuration- Returns:
- calculated sample size
-
getFileNameWithoutExtension
public static java.lang.String getFileNameWithoutExtension(java.lang.String fileNameWithExtension)
Gets the absolute Filename without the extension.- Parameters:
fileNameWithExtension
- fileName with extension (e.g. VIDEO_01.mp4)- Returns:
- file name without extension
-
rotateBitmap
public static android.graphics.Bitmap rotateBitmap(android.graphics.Bitmap bitmap, int orientation)
Rotate the bitmap with the specified orientation.- Parameters:
bitmap
-Bitmap
to rotateorientation
- type of orientation to execute- Returns:
Bitmap
with rotated configuration
-
createImageThumbnail
public static android.graphics.Bitmap createImageThumbnail(java.lang.String filePath, int kind)
This method first examines if the thumbnail embedded in EXIF is bigger than our target size. If not, then it'll create a thumbnail from original image. Due to efficiency consideration, we want to let MediaThumbRequest avoid calling this method twice for both kinds, so it only requests for MICRO_KIND and set saveImage to true. This method always returns a "square thumbnail" for MICRO_KIND thumbnail. This method is only used by media framework and media provider internally.- Parameters:
filePath
- the path of image filekind
- could be MINI_KIND or MICRO_KIND- Returns:
- Bitmap, or null on failures
-
extractThumbnail
public static android.graphics.Bitmap extractThumbnail(android.graphics.Bitmap source, int width, int height)
Creates a centered bitmap of the desired size.- Parameters:
source
- original bitmap sourcewidth
- targeted widthheight
- targeted height- Returns:
- Bitmap, or null on failures
-
extractThumbnail
public static android.graphics.Bitmap extractThumbnail(android.graphics.Bitmap source, int width, int height, int options)
Creates a centered bitmap of the desired size.- Parameters:
source
- original bitmap sourcewidth
- targeted widthheight
- targeted heightoptions
- options used during thumbnail extraction- Returns:
- Bitmap, or null on failures
-
rotateImageIfRequired
public static android.graphics.Bitmap rotateImageIfRequired(android.graphics.Bitmap img, android.net.Uri selectedImage) throws java.io.IOException
Rotate an image if required.- Parameters:
img
- The image bitmapselectedImage
- Image URI- Returns:
- The resulted Bitmap after manipulation
- Throws:
java.io.IOException
- failed accessing files
-
rotateImage
public static android.graphics.Bitmap rotateImage(android.graphics.Bitmap img, int degree)
Rotates an image to a specified degree.- Parameters:
img
-Bitmap
to rotatedegree
- rotation level- Returns:
- rotated
Bitmap
-
getThumbnail
public static android.graphics.Bitmap getThumbnail(android.content.Context context, android.net.Uri uri) throws java.io.FileNotFoundException, java.io.IOException
Returns the thumbnail image from a given uri.- Parameters:
context
- used to get resourceuri
-Uri
to be used- Returns:
- thumbnail as
Bitmap
- Throws:
java.io.FileNotFoundException
- if passing an invalid urijava.io.IOException
- if rejection any connection to the path
-
createFileFromBitmap
public static java.io.File createFileFromBitmap(android.app.Activity activity, android.graphics.Bitmap source)
Creates a temporary image file in app's cache- Parameters:
activity
- Activity of the appsource
- Bitmap of the image to be created- Returns:
- File object
-
createTempImageFile
public static java.io.File createTempImageFile(android.content.Context context, android.graphics.Bitmap image)
Create Temporary Image File with filename accdg to date created- Parameters:
context
- used to get resourcesimage
-Bitmap
file to be used- Returns:
- temporary
File
-
parseMediaUris
public static java.io.File[] parseMediaUris(java.lang.String[] uriStrings)
Parses array of string uris- Parameters:
uriStrings
- array of uri- Returns:
- file format of uri
-
saveBitmap
public static java.io.File saveBitmap(android.content.Context context, android.graphics.Bitmap bitmap, java.lang.String fileName, int compressQuality)
Saves bitmap in the files directory- Parameters:
context
- contextbitmap
- bitmap to savefileName
- filename of bitmapcompressQuality
- Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting- Returns:
- directory of the saved bitmap
-
downloadImage
public static java.lang.String downloadImage(android.content.Context context, android.net.Uri selectedImage)
Download and save an image- Parameters:
context
- used to get resourcesselectedImage
-Uri
of the selected image- Returns:
- path of the file downloaded
-
getImageUri
public static java.lang.String getImageUri(android.content.Context context, android.graphics.Bitmap inImage)
Find the file path of theBitmap
- Parameters:
context
- used to get resourcesinImage
-Bitmap
used for lookup- Returns:
- path of the file
-
getFilePathForN
public static java.lang.String getFilePathForN(android.content.Context context, android.net.Uri uri)
Find file path for N or higher OS veersion- Parameters:
context
- used to get resourcesuri
-Uri
of the file- Returns:
- file path
-
-