Thursday 20 June 2013

[Solved] - Nexus 7 Doesn't Show Up As Media Device (MTP)

Solution To Nexus 7 Doesn't Show Up As Media Device (MTP)

1.       Plug the Nexus 7 into your computer with the USB cable.
2.       Open Device Manager.
3.       Find "Android Device" near the top and expand the node. Double click on "Android Composite ADB Interface".
4.       In the properties window that pops up, uninstall the driver (chose to delete the current driver as well).

5.       Unplug the tablet and plug it back in.

Scaling images to given width and height with maintaining it's aspect ratio in android


Scaling images to given width and height with maintaining it's aspect ratio in android 


Following code can be used to scale images to given width and height with maintaining it's aspect ratio

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class BitmapSizeHelper {

public static enum ScalingLogic {
CROP, FIT
}

public static Bitmap getBitmapFromResources(Resources res, int resId,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth,
options.outHeight, dstWidth, dstHeight, scalingLogic);
Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId,
options);

return unscaledBitmap;

}

public static Bitmap getBitmapFromPath(int targetW, int targetH,
String photopath, int dstWidth, int dstHeight,
ScalingLogic scalingLogic) {

Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photopath, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth,
options.outHeight, dstWidth, dstHeight, scalingLogic);
Bitmap bitmap = BitmapFactory.decodeFile(photopath, options);

return bitmap;

}

public static int calculateSampleSize(int srcWidth, int srcHeight,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;

if (srcAspect > dstAspect) {
return srcWidth / dstWidth;
} else {
return srcHeight / dstHeight;
}
} else {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;

if (srcAspect > dstAspect) {
return srcHeight / dstHeight;
} else {
return srcWidth / dstWidth;
}
}
}

public static Bitmap createScaledBitmap(Bitmap unscaledBitmap,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(),
unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(),
unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(),
dstRect.height(), Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(
Paint.FILTER_BITMAP_FLAG));

return scaledBitmap;
}

public static Rect calculateSrcRect(int srcWidth, int srcHeight,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.CROP) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;

if (srcAspect > dstAspect) {
final int srcRectWidth = (int) (srcHeight * dstAspect);
final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth,
srcHeight);
} else {
final int srcRectHeight = (int) (srcWidth / dstAspect);
final int scrRectTop = (int) (srcHeight - srcRectHeight) / 2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop
+ srcRectHeight);
}
} else {
return new Rect(0, 0, srcWidth, srcHeight);
}
}

public static Rect calculateDstRect(int srcWidth, int srcHeight,
int dstWidth, int dstHeight, ScalingLogic scalingLogic) {
if (scalingLogic == ScalingLogic.FIT) {
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;

if (srcAspect > dstAspect) {
return new Rect(0, 0, dstWidth, (int) (dstWidth / srcAspect));
} else {
return new Rect(0, 0, (int) (dstHeight * srcAspect), dstHeight);
}
} else {
return new Rect(0, 0, dstWidth, dstHeight);
}
}
}

Now we can use ablove code in two ways:

1.  If we have any image with any size and we want to show it completely inside a fixed size of imageview we can do it like:

                        Drawable drawable = Drawable.createFromPath(brandPrimaryImagePath);
if(null!= drawable){
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
Bitmap scaledBitmap =  BitmapSizeHelper.createScaledBitmap(bitmap, x, y, BitmapSizeHelper.ScalingLogic.FIT);
                        }

where xy are the width and height of imageview respectively.


2. If we have any image with any size and we want to show it cropped inside a fixed size of imageview we can do it like:

                        Drawable drawable = Drawable.createFromPath(brandPrimaryImagePath);
if(null!= drawable){
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
Bitmap scaledBitmap =  BitmapSizeHelper.createScaledBitmap(bitmap, xy, BitmapSizeHelper.ScalingLogic.CROP);
                        }

where xare the width and height of imageview respectively.