Android Camera Exif no orientation data but Image is rotated - android

I am using following helper class to handle sampling and rotation of the Camera Image.
object CaptureImageHelper {
/**
* This method is responsible for solving the rotation issue if exist. Also scale the images to
* 1024x1024 resolution
*
* #param context The current context
* #param selectedImage The Image URI
* #return Bitmap image results
* #throws IOException
*/
#Throws(IOException::class)
fun handleSamplingAndRotationBitmap(
context: Context,
selectedImage: Uri?,
isFrontCamera: Boolean
): Bitmap? {
val MAX_HEIGHT = 1024
val MAX_WIDTH = 1024
// First decode with inJustDecodeBounds=true to check dimensions
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
var imageStream: InputStream = context.getContentResolver().openInputStream(selectedImage!!)!!
BitmapFactory.decodeStream(imageStream, null, options)
imageStream.close()
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, MAX_WIDTH, MAX_HEIGHT)
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false
imageStream = context.getContentResolver().openInputStream(selectedImage!!)!!
var img = BitmapFactory.decodeStream(imageStream, null, options)
img = rotateImageIfRequired(img!!, selectedImage, isFrontCamera)
return img
}
/**
* Calculate an inSampleSize for use in a [BitmapFactory.Options] object when decoding
* bitmaps using the decode* methods from [BitmapFactory]. This implementation calculates
* the closest inSampleSize that will result in the final decoded bitmap having a width and
* height equal to or larger than the requested width and height. This implementation does not
* ensure a power of 2 is returned for inSampleSize which can be faster when decoding but
* results in a larger bitmap which isn't as useful for caching purposes.
*
* #param options An options object with out* params already populated (run through a decode*
* method with inJustDecodeBounds==true
* #param reqWidth The requested width of the resulting bitmap
* #param reqHeight The requested height of the resulting bitmap
* #return The value to be used for inSampleSize
*/
private fun calculateInSampleSize(
options: BitmapFactory.Options,
reqWidth: Int, reqHeight: Int
): Int {
// Raw height and width of image
val height = options.outHeight
val width = options.outWidth
var inSampleSize = 1
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
val heightRatio =
Math.round(height.toFloat() / reqHeight.toFloat())
val widthRatio =
Math.round(width.toFloat() / reqWidth.toFloat())
// Choose the smallest ratio as inSampleSize value, this will guarantee a final image
// with both dimensions larger than or equal to the requested height and width.
inSampleSize = if (heightRatio < widthRatio) heightRatio else widthRatio
// This offers some additional logic in case the image has a strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might still
// end up being too large to fit comfortably in memory, so we should
// be more aggressive with sample down the image (=larger inSampleSize).
val totalPixels = width * height.toFloat()
// Anything more than 2x the requested pixels we'll sample down further
val totalReqPixelsCap = reqWidth * reqHeight * 2.toFloat()
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++
}
}
return inSampleSize
}
/**
* Rotate an image if required.
*
* #param img The image bitmap
* #param selectedImage Image URI
* #return The resulted Bitmap after manipulation
*/
#Throws(IOException::class)
private fun rotateImageIfRequired(
img: Bitmap,
selectedImage: Uri,
isFrontCamera: Boolean
): Bitmap? {
val ei = ExifInterface(selectedImage.path!!)
val orientation: Int =
ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
return when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> rotateImage(img, 90, isFrontCamera)
ExifInterface.ORIENTATION_ROTATE_180 -> rotateImage(img, 180, isFrontCamera)
ExifInterface.ORIENTATION_ROTATE_270 -> rotateImage(img, 270, isFrontCamera)
else -> img
}
}
private fun rotateImage(
img: Bitmap,
degree: Int,
isFrontCamera: Boolean
): Bitmap? {
val matrix = Matrix()
if(isFrontCamera) {
val matrixMirrorY = Matrix()
val mirrorY = floatArrayOf(-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f)
matrixMirrorY.setValues(mirrorY)
matrix.postConcat(matrixMirrorY)
matrix.preRotate(270f)
} else {
matrix.postRotate(degree.toFloat())
}
val rotatedImg =
Bitmap.createBitmap(img, 0, 0, img.width, img.height, matrix, true)
img.recycle()
return rotatedImg
}
}
Calling helper class
val bitmap = CaptureImageHelper.handleSamplingAndRotationBitmap(requireContext(), Uri.fromFile(cameraImage!!), false)
The issue i am getting is random. Mostly if I get rotated image below statement
ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)
returns ExifInterface.ORIENTATION_ROTATE_90 which is fine and code rotate that image correctly. But sometimes Image is rotated but it Exif getAttributeInt returns ExifInterface.ORIENTATION_NORMAL. Which i believe means there is no data of Exif/Orientation against this image and it returns default value.
Exit get Attribute method
public int getAttributeInt(#NonNull String tag, int defaultValue) {
if (tag == null) {
throw new NullPointerException("tag shouldn't be null");
}
ExifAttribute exifAttribute = getExifAttribute(tag);
if (exifAttribute == null) {
return defaultValue;
}
try {
return exifAttribute.getIntValue(mExifByteOrder);
} catch (NumberFormatException e) {
return defaultValue;
}
}

Related

Separate YUV buffers to Bitmap in Android

As a brief context, I am getting raw video data from Zoom SDK as separate Y, U, and V ByteBuffers and trying to convert them to bitmaps.
However, I noticed that this conversion is resulting in grayscale bitmap images with green and pink spots [as shown in the screenshot below]
the method I’m using for conversion is the following:
fun ZoomVideoSDKVideoRawData.toBitmap(): Bitmap? {
val width = streamWidth
val height = streamHeight
val yuvBytes = ByteArray(width * (height + height / 2))
val yPlane = getyBuffer()
val uPlane = getuBuffer()
val vPlane = getvBuffer()
// copy Y
yPlane.get(yuvBytes, 0, width * height)
// copy U
var offset = width * height
uPlane.get(yuvBytes, offset, width * height / 4)
// copy V
offset += width * height / 4
vPlane.get(yuvBytes, offset, width * height / 4)
// make YUV image
val yuvImage = YuvImage(yuvBytes, ImageFormat.NV21, width, height, null)
// convert image to bitmap
val out = ByteArrayOutputStream()
yuvImage.compressToJpeg(Rect(0, 0, width, height), 50, out)
val imageBytes = out.toByteArray()
val image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
// return result
return image
}
any idea why the colors of the resulting bitmap are corrupted?
Thanks in advance!

Choose image size in camera intent

I'm new to this forum, so please bear with me and gently point out mistakes if any,
So I'm working on a project where I'm uploading images to server, now I want to limit the size of images, I'm giving an option to "Click image" where my code will open default camera intent and clicks the pic, or "Choose from gallery".
My question is regarding "Click image", Now when user clicks an image, can I preset the image max size which can be clicked?
You can resize your image in onActivityResult method,try following code snippet
public static Bitmap handleSamplingAndRotationBitmap(Context context, Uri selectedImage)
throws IOException {
int MAX_HEIGHT = 1024;
int MAX_WIDTH = 1024;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream imageStream = context.getContentResolver().openInputStream(selectedImage);
BitmapFactory.decodeStream(imageStream, null, options);
imageStream.close();
options.inSampleSize = calculateInSampleSizes(options, MAX_WIDTH, MAX_HEIGHT);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
imageStream = context.getContentResolver().openInputStream(selectedImage);
Bitmap img = BitmapFactory.decodeStream(imageStream, null, options);
img = rotateImageIfRequired(context, img, selectedImage);
return img;
}
private static int calculateInSampleSizes(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee a final image
// with both dimensions larger than or equal to the requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
// This offers some additional logic in case the image has a strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might still
// end up being too large to fit comfortably in memory, so we should
// be more aggressive with sample down the image (=larger inSampleSize).
final float totalPixels = width * height;
// Anything more than 2x the requested pixels we'll sample down further
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
}
return inSampleSize;
}
private static Bitmap rotateImageIfRequired(Context context, Bitmap img, Uri selectedImage) throws IOException {
InputStream input = context.getContentResolver().openInputStream(selectedImage);
ExifInterface ei;
if (Build.VERSION.SDK_INT > 23)
ei = new ExifInterface(input);
else
ei = new ExifInterface(selectedImage.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
}
private static Bitmap rotateImage(Bitmap img, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img.recycle();
return rotatedImg;
}
you just need invoke handleSamplingAndRotationBitmap method,you'll get a Bitmap which size can be set by yourself.
PS: Case some pictures captured by sumsung's phone 's rotation is incorrect,so we need handle picture's orientation too,hope that can help you.
You can simply get the size of the file. You need to store the image when u take for it. after that u can get size using below code segment
String imagePath = Environment.getExternalStorageDirectory() + "/yourImagefile.png";
File imageFile = new File(imagePath );
long filelength = imageFile .length();
length = filelength/1024;
this length give you size in KB. then you can add if condition like below
if(length>sizeyouwant){
//delete image and toast message with info
if(imageFile.exists()) {
imageFile.delete();
}
Toast.makeText(getApplicationContext(),
"Image is not saved due to image size exceeds limit....",
Toast.LENGTH_SHORT).show();
}

How to display properly sized image from user gallery?

I am struggling with showing the proper sized images in two of my activities.
The image in the CatalogActivity looks like it is properly scaled down and then cropped while the image in the EditorActivity is whole and is much smaller.
The only difference is that the image in the catalog comes from image resource while the editor image comes from the gallery so from Uri.
Could you tell me why is this difference and how to make the image from EditorActivity the same as the other one?
From CatalogActivity:
// Make the sample image smaller so it doesn't take too much space in the memory
Bitmap sourdoughBitmap = ProductsEntry.decodeSampledBitmapFromResource(getResources(), R.drawable.sourdough_picture, 72, 72);
From EditorActivity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == RESULT_LOAD_PICTURE && resultCode == RESULT_OK && null != intent) {
Uri selectedPictureUri = intent.getData();
// Show the selected picture in an ImageView in the editor
try {
// Scale the bitmap received from the uri so it fits in the small ImageView
scaledPictureBitmap = ProductsEntry.decodeSampledBitmapFromUri(this, selectedPictureUri, 72, 72);
// Hide the gray picture placeholder
mImageView.setBackgroundResource(0);
// Show the scaled bitmap in the ImageView
mImageView.setImageBitmap(scaledPictureBitmap);
// The user has chosen a picture and we can change
// the text of the button to say "Change picture"
mAddPictureButton.setText(R.string.edit_product_change_photo);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
From ProductsEntry:
/**
* Helps create a smaller bitmap image from resource
* #param resources - a resources object
* #param resourceId - the id of the image in the drawable folder
* #param requiredWidth - the width that we want for the final image
* #param requiredHeight - the height that we want for the final image
* #return the decoded Bitmap
*/
public static Bitmap decodeSampledBitmapFromResource(Resources resources, int resourceId,
int requiredWidth, int requiredHeight){
// First decode with inJustDecodeBounds = true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, resourceId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
// Decode bitmap with inJustDecodeBounds = false
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(resources, resourceId, options);
}
/**
* Helps create a smaller bitmap image from a uri
* #param context - a context object
* #param uri - the uri of the image
* #param requiredWidth - the width that we want for the final image
* #param requiredHeight - the height that we want for the final image
* #return the decoded Bitmap
*/
public static Bitmap decodeSampledBitmapFromUri(Context context, Uri uri, int requiredWidth, int requiredHeight)
throws FileNotFoundException {
// First decode with inJustDecodeBounds = true, only to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight);
// Decode bitmap with inJustDecodeBounds = false
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri), null, options);
}
/**
* Calculate a sample size value that is a power of 2 based on a target width and height
* #param options is used to pass options to the BitmapFactory
* #param requiredWidth is the width that we want for the final image
* #param requiredHeight is the height that we want for the final image
* #return by how much to scale down the image
*/
private static int calculateInSampleSize(BitmapFactory.Options options, int requiredWidth, int requiredHeight) {
// Raw height and width of the image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > requiredHeight || width > requiredWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the required height and width
while ((halfHeight / inSampleSize) >= requiredHeight
&& (halfWidth / inSampleSize) >= requiredWidth){
inSampleSize *= 2;
}
}
return inSampleSize;
}
From activity_editor.xml:
<ImageButton
android:id="#+id/picture"
android:layout_height="72dp"
android:layout_width="72dp"
android:src="#drawable/ic_photo"
android:layout_gravity="center" />
Link to the repository here.

ImageView dimensions

I have following situation:
I have ImageView and Bitmap inside it.
The problem is that
I don't know the dimensions of bitmap (not physical size, size that it takes on screen)
or
I need ImageView not to be bigger that this Bitmap on the screen.
ImageView's background has black color on below snapshot.
You can make sure the image view isn't bigger than the bitmap by setting its layout_width and layout_height to wrap_content on your imageView tag in its xml file.
You can also use its scaleType to affect how the image should be manipulated to fit the imageView.
You can also just access the bitmap's width/height properties to get its dimensions.
EDIT::
You can convert your bitmap into a byte[] and resize it using the following helpers:
/**
* Resize an image to a specified width and height.
* #param targetWidth The width to resize to.
* #param targetHeight The height to resize to.
* #return The resized image as a Bitmap.
* */
public static Bitmap resizeImage(byte[] imageData, int targetWidth, int targetHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = calculateInSampleSize(options, targetWidth, targetHeight);
options.inJustDecodeBounds = false;
Bitmap reducedBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
return Bitmap.createScaledBitmap(reducedBitmap, targetWidth, targetHeight, false);
}
private static int calculateInSampleSize(BitmapFactory.Options options, int requestedWidth, int requestedHeight) {
// Get the image's raw dimensions
final int rawHeight = options.outHeight;
final int rawWidth = options.outWidth;
int inSampleSize = 1;
if (rawHeight > requestedHeight || rawWidth > requestedWidth) {
final int halfHeight = rawHeight / 2;
final int halfWidth = rawWidth / 2;
/*
* Calculate the largest inSampleSize value that is a power of 2 and keeps both
* height and width larger than their requested counterparts respectively.
* */
while ((halfHeight/inSampleSize) > requestedHeight && (halfWidth/inSampleSize) > requestedWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}

center Crop Image In proper size to set on ImageView

I am using camera API to take picture i have to open camera in different sizes according to my Image view size. I am following the sample project which we get inside Android sdk/sample/adroid-18 at the name "ApiDemo" the thing i have changed is not set camera on setcontentview. I have set the camera on Frame Layout. at first my camera preview was starched so i got the camera OptimalPreviewSize and make FrameLayout parameter width and height as wrap-content.Now the camera preview is smaller then ImageView (The size i want). If i make the size of FrameLayout parameter as match-parent then camera View is stretch.How to resolve this issue.
find this link for more specification. Android camera preview look strange
UPDATE
My camera preview size is fine now i use the on Layout method the idea was i have the bigger layout then my ImageView and now camera preview is looking good.
Now the Problem I am facing is set the image of proper size for this I have to center crop and scale in same size in like my ImageView.this Image i get by TakePicture method and saved in sdcard.
For this I am using this method:-
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left+50, top, left + scaledWidth, top + scaledHeight+50);
// RectF targetRect = new RectF(0, 0, newWidth, newHeight/2);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
But the result image quality is not good.Height Corners are cutting from top and bottom, and result image quality is not good.Pixels are stretching.
Don't tell me to use scaleType=Center_crop i can't use it in my case,and don't want to show cropping frame to user,this all process should not show on UI.
UPDATE
I used blow method for crop image from center and scale according to my imageView size
Bitmap dstBmp = ThumbnailUtils.extractThumbnail(source, newWidth, newHeight);
But the bitmap i got is not looking same the camera preview shown on FrameLayout. because camera preview is big.I think these code cropped the large area.
I tried to reduce the width and change the height but not getting the same cropped image in which ratio i want.
One more idea i have after picture crop a last image frame set automatically on FrameLayout. can we get that set frame from Frame Layout. How is this possible?
Here is question like this How to retrieve the visible part of a SurfaceView in Android do any one have solution.
I want to achieve this by this line ThumbnailUtils.extractThumbnail(source, newWidth, newHeight);and by this line i am getting src like image described in diagram .
What to change in this line exactly ????
Center crop an image may be help you this.
public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);
return dest;
}
#Akanksha Please use this below code, you just need to pass the path of the saved image, and the hight and width of our imageview. This code works perfectly for me.
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ImageHandler {
/**
* Decode and sample down a bitmap from a file to the requested width and
* height.
*
* #param filename
* The full path of the file to decode
* #param reqWidth
* The requested width of the resulting bitmap
* #param reqHeight
* The requested height of the resulting bitmap
* #return A bitmap sampled down from the original with the same aspect
* ratio and dimensions that are equal to or greater than the
* requested width and height
*/
public static Bitmap decodeSampledBitmapFromFile(String filename,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filename, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filename, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
// This offers some additional logic in case the image has a
// strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might
// still
// end up being too large to fit comfortably in memory, so we
// should
// be more aggressive with sample down the image (=larger
// inSampleSize).
final float totalPixels = width * height;
// Anything more than 2x the requested pixels we'll sample down
// further.
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
}
return inSampleSize;
}
}
I call this method inside async task because it may take too much UImemory and time
Here is how I call it.
class Asyncing extends AsyncTask {
private int reqWidth;
private int reqHeight;
private ImageView iv;
private String fileName;
private ProgressDialog pd;
public Asyncing(int reqWidth, int reqHeight, ImageView iv,
String fileName) {
super();
this.reqWidth = reqWidth;
this.reqHeight = reqHeight;
this.fileName = fileName;
this.iv = iv;
}
#Override
protected Bitmap doInBackground(String... params) {
return ImageHandler.decodeSampledBitmapFromFile(params[0],
reqWidth, reqHeight);
}
#Override
protected void onPostExecute(Bitmap result) {
iv.setImageBitmap(result);
if (pd.isShowing()) {
pd.setMessage(getString(R.string.completed));
pd.dismiss();
}
super.onPostExecute(result);
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
#Override
protected void onPreExecute() {
pd = ProgressDialog.show(CustomerDetailsActivity.this, "",
getString(R.string.processing_signature));
super.onPreExecute();
}
}
This is how you need to call the asynctask
signedImagePath = data.getExtras().getString("imagePath");
new Asyncing(signature_img.getWidth(), signature_img.getHeight(),
signature_img, "spenTest.png").execute(signedImagePath);
above code is written according to my requirements,you modify it according to yours.

Categories

Resources