I am developing an application that shows the different photos from the server and the user can set selected photos as wallpaper of its device I used the given code to set wallpaper it works but the image was not set correctly it does not fit to screen. I used this code.
String dirPath = getFilesDir().toString();
String folder = mPhotos.get(nextPosition - 1).getCategory();
String filePath = dirPath + "/PhotoViewer/" + folder + "/"
+ mPhotos.get(nextPosition - 1).getFileName();
File imageFile = new File(filePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile
.getAbsolutePath());
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try
{
myWallpaperManager.setBitmap(bitmap);
Toast.makeText(PhotoActivity.this, "Wallpaper set",
Toast.LENGTH_SHORT).show();
} catch(IOException e){
Toast.makeText(PhotoActivity.this, "Error setting wallpaper",
Toast.LENGTH_SHORT).show();
}
To set wallpaper in android use the below code: By using WallpaperManager Class
Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);
buttonSetWallpaper.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.five);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Need to set permission in Manifest:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
You can try resizing your bitmap like this
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels << 1; // best wallpaper width is twice screen width
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(path, options);
WallpaperManager wm = WallpaperManager.getInstance(this);
try {
wm.setBitmap(decodedSampleBitmap);
} catch (IOException e) {
Log.e(TAG, "Cannot set image as wallpaper", e);
}
Your calculateInSampleSize class can be like this
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) {
// 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;
}
return inSampleSize;
}
Refer this link for further clarifications
Related
I found a lot of documentation on how to load large Bitmaps and avoid outofmemory exception. but the problem is that I have to take the image from my MediaStore.Images.media so the classical
decodeFile(path,options) indicated in the google documentation does not work to me
As you can see below I decommented the line // Bitmap photo= Mediastore.Images, that is the one that triggers the out of memory. on the other side adding
the line Bitmap bm=BitmapFactory.decodeFile(selectedImageToUri,options) returns null, although the compiler can see both the path in selectedImageToUri (that indicates the content provider where the pics are) than the options value, that I set to 8, because I want to subscale all the images
My question is how can I insert in bm the bitmap that is referring to the image selected by the user in the gallery. in the line BitMap photo does not return null and work really well, but I decommented because after I change a couple of images gives me outofmemory exception.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable final Bundle savedInstanceState) {
if (flagVariable) {
if (selectedImageToUri != null) {
// BitMap photo = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), Uri.parse(selectedImageToUri));
final BitmapFactory.Options options= new BitmapFactory.Options();
options.inSampleSize=8;
Bitmap bm = BitmapFactory.decodeFile(selectedImageToUri, options);
pic = new BitmapDrawable(bm);
getActivity().getWindow().setBackgroundDrawable(pic);
} else {
getDefaultImageBackground(inflater, container);
}
hiddenList = inflater.inflate(R.layout.fragment_as_list_layout_temp, container, false);
} else {
getDefaultImageBackground(inflater, container);
}
listView = (ListView) hiddenList.findViewById(R.id.list_hidden);
MediaStore.getBitmap is just a simple convienence method, it looks like this:
public static final Bitmap getBitmap(ContentResolver cr, Uri url)
throws FileNotFoundException, IOException {
InputStream input = cr.openInputStream(url);
Bitmap bitmap = BitmapFactory.decodeStream(input);
input.close();
return bitmap;
}
You can create your own method based on this that takes the options and calls a different overload on BitmapFactory:
public static final Bitmap getBitmap(ContentResolver cr,
Uri url,
BitmapFactory.Options options)
throws FileNotFoundException, IOException {
InputStream input = cr.openInputStream(url);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, options);
input.close();
return bitmap;
}
Usage:
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bm = getBitmap(getActivity().getContentResolver(),
Uri.parse(selectedImageToUri),
options);
I spent a lot of time on this problem, but no one will give me exact answer and finally i solved it. First create method and provide Image URI as argument, and this will return bitmap basically here i calculated image size on bases of, we can manage memory as well as image and get exact image in bitmap form.
you can even display 5000×8000 and 12MiB picture without any error code is tested just copy paste in your class and enjoy.
Use
Bitmap mBitmap = getPhoto(MYIMAGEURI);
Provide URI to method and get Bitmap
Bitmap getPhoto(Uri selectedImage) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap photoBitmap = null;
InputStream inputStream = null;
try {
inputStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, bitmapOptions);
int imageWidth = bitmapOptions.outWidth;
int imageHeight = bitmapOptions.outHeight;
#SuppressWarnings("unused")
InputStream is = null;
try {
is = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
float scale = 1.0f;
if (imageWidth < imageHeight) {
if (imageHeight > width * 1.0f) {
scale = width * 1.0f / (imageHeight * 1.0f);
}
} else {
if (imageWidth > width * 1.0f) {
scale = width * 1.0f / (imageWidth * 1.0f);
}
}
photoBitmap = decodeSampledBitmapFromResource(this,
selectedImage, (int) (imageWidth * scale),
(int) (imageHeight * scale));
return photoBitmap;
}
Decode Bitmap Sample using image size
public static Bitmap decodeSampledBitmapFromResource(Context context,
Uri uri, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream is = null;
try {
is = context.getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BitmapFactory.decodeStream(is, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode editBitmap with inSampleSize set
options.inJustDecodeBounds = false;
InputStream inputs = null;
try {
inputs = context.getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return BitmapFactory.decodeStream(inputs, null, options);
}
Calculate Sample Size
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) {
// 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 = Math.min(heightRatio, widthRatio);
// inSampleSize = heightRatio < widthRatio ? heightRatio :
// widthRatio;
}
return inSampleSize;
}
Or may be possible to solved using one line of code in manifiest.xml
is in application tag use this
android:largeHeap="true"
I am having a strange behaviour when trying to decode a photo of the size 2448x2448 pixels. In code, I am calculating that a inSampleSize of 6 should be applied (based on the required size of the resulting bitmap) and when I call BitmapFactory.decodeStream with those options I am expecting a bitmap like this:
full_photo_width = 2448
full_photo_height = 2448
inSampleSize = 6
expected_width = (2448 / 6) = 408
expected_height (2448 / 6) = 408
actual_width = 612
actual_height = 612
Here is the code:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int photo_width = options.outWidth;
int photo_height = options.outHeight;
float rotation = rotationForImage(this, uri);
if (rotation != 0f) {
// Assume the photo is portrait oriented
matrix.preRotate(rotation);
float photo_ratio = (float) ((float)photo_width / (float)photo_height);
frame_height = (int) (frame_width / photo_ratio);
} else {
// Assume the photo is landscape oriented
float photo_ratio = (float) ((float)photo_height / (float)photo_width);
frame_height = (int) (frame_width * photo_ratio);
}
int sampleSize = calculateInSampleSize(options, frame_width, frame_height);
if ((sampleSize % 2) != 0) {
sampleSize++;
}
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And the calculateInSampleSize function:
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) {
// 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);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
// We round the value to the highest, always.
if ((height / inSampleSize) > reqHeight || (width / inSampleSize > reqWidth)) {
inSampleSize++;
}
}
return inSampleSize;
}
The code works for all the photos for all the photos and decodeStream is returning a bitmap with the correct size (depending on the calculated inSampleSize) in all the cases, except with a particular photo. Am I missing something here?
Thanks!
Please refer to official API documentation: inSampleSize.
Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.
I am trying to decode the image before drawing it in Canvas. The procedure I followed is same as mentioned in http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Issue :
The image is scaled only to 70% of the screen. Can you please let me know if I miss any parameters ?
public Bitmap getAssetImage(Context context, String filename) throws IOException {
//Decode image size
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.backgroundhomepage, options);
//The new size we want to scale to
final int REQUIRED_WIDTH= (int) dWidth;
final int REQUIRED_HIGHT= (int) dHeight;
//Find the correct scale value. It should be the power of 2.
int inSampleSize=1;
if (options.outHeight > REQUIRED_HIGHT || options.outWidth > REQUIRED_WIDTH) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) options.outHeight / (float) REQUIRED_HIGHT);
final int widthRatio = Math.round((float) options.outWidth / (float) REQUIRED_WIDTH);
// 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;
}
/*
while(options.outWidth/inSampleSize/2>=REQUIRED_WIDTH && options.outHeight/inSampleSize/2>=REQUIRED_HIGHT)
inSampleSize*=2; */
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=inSampleSize;
o2.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.backgroundhomepage, o2);
}
public void run() {
// TODO Auto-generated method stub
// Log.i("Notice", "In run of mybringback");
if(backgoundImage == null){
try {Log.i("MyBringBack", "In run of mybringback.. getting the image of background");
backgoundImage = getAssetImage(getApplicationContext(),"backgroundhomepage");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ourHolder = getHolder();
while (isRunning) {
// Log.i("DragDrop", "ourHolder.getSurface().isValid()" + ourHolder.getSurface().isValid() );
if (!ourHolder.getSurface().isValid()){
continue;
}
canvas = ourHolder.lockCanvas();
screenCenterX = dWidth / 2;
screenCenterY = dHeight / 2;
canvas.drawBitmap(backgoundImage, 0, 0, null);
if (imagePublishDone) {
if(!welcomeDone){
message = "Drop your wish to panda";
tts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
welcomeDone=true;
}
moveImageInEllipticalPath();
} else {
initialImagePublish();
}
centreReached = false;
ourHolder.unlockCanvasAndPost(canvas);
}
}
final int REQUIRED_WIDTH= Screen_width;
final int REQUIRED_HIGHT= screen_height;
Calculate screen dimensions first
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screen_width = size.x;
int screen_height = size.y;
In my application I have to capture image from camera or import from gallery, show it in imageview in activity. Everything is fine, I am getting image from both and able to set it on imageview without any exception. But sometimes image is not getting scaled properly and gets vertically stretched or with orientation changed. Please help me out.
Here is my code to decode image referred from official android documentation:
public static Bitmap decodeSampledBitmapFromResource(File photoFile, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(new FileInputStream(photoFile), null,
options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(new FileInputStream(photoFile),
null, options);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
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) {
// 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;
}
return inSampleSize;
}
The images has different orientations so it rotates according to the orientation when putting on imageview. You can check the orientation of the photo from properties of image.
To set the image in proper manner you can use the following code...
int rot=getCameraPhotoOrientation(this,Uri,picturePath);
if(rot!=0)
bitmap=new RotateOrientation().RotateOrientationCall(bitmap,rot);
The getCameraPhotoOrientation Method:-
public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
int rotate = 0;
try {
context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(
imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
Log.d(TAG, "Exit orientation: " + orientation);
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
Add RotateOrientation class to rotate class according to orientation.
public class RotateOrientation {
Bitmap RotateOrientationCall(Bitmap src,float degree)
{
Matrix matrix=new Matrix();
matrix.postRotate(degree);
Bitmap bmOut = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
return bmOut;
}
}
I am displaying images from the string path (which I retrieve from database) and I have no problem displaying one image. But when I have 4 images, only one image (the first image) gets displayed and the others are not displayed. My code is as followed:
try{
name.setText(nameString);
school.setText(schoolString);
psupervisor.setText(info.getPsupervisor());
pdate.setText(info.getPdate());
a.setChecked(Boolean.parseBoolean(info.getPtick1()));
b.setChecked(Boolean.parseBoolean(info.getPtick2()));
c.setChecked(Boolean.parseBoolean(info.getPtick3()));
pcomment1.setText(info.getPcomment1());
psignature1.setImageBitmap(resizeSignatureBitmap(info.getPsignature1()));
pcomment2.setText(info.getPcomment2());
psignature2.setImageBitmap(resizeSignatureBitmap(info.getPsignature2()));
pdate2.setText(info.getPdate2());
d.setChecked(Boolean.parseBoolean(info.getPtick4()));
e.setChecked(Boolean.parseBoolean(info.getPtick5()));
f.setChecked(Boolean.parseBoolean(info.getPtick6()));
pcomment3.setText(info.getPcomment3());
psignature3.setImageBitmap(resizeSignatureBitmap(info.getPsignature3()));
pcomment4.setText(info.getPcomment4());
psignature4.setImageBitmap(resizeSignatureBitmap(info.getPsignature4()));
Log.d("PREPOST: ", log2);
db.close();
}
catch(Exception ex){}
}
//method to resize the bitmap before displaying
public Bitmap resizeSignatureBitmap(String imagePath){
BitmapFactory.Options options = new BitmapFactory.Options();
InputStream is = null;
try {
is = new FileInputStream(imagePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.decodeStream(is,null,options);
try {
is.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
is = new FileInputStream(imagePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// here w and h are the desired width and height
options.inSampleSize = Math.max(options.outWidth/w, options.outHeight/h);
// bitmap is the resized bitmap
Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);
return bitmap;
}
Is there anything wrong with what I'm doing? Please kindly point my mistake out as I can't find my mistake. No errors, nothing, just that it doesn't get displayed. By default, it only displays one image and it's always the first image (psignature1) and if I comment it out, then second image (psignature2) would get displayed but the rest would not be displayed.
Thank you for your time!
//method to resize the bitmap before displaying
public Bitmap resizeSignatureBitmap(String imagePath){
BitmapFactory.Options options = new BitmapFactory.Options();
InputStream is = null;
try {
is = new FileInputStream(imagePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// here w and h are the desired width and height
options.inSampleSize = Math.max(options.outWidth/w, options.outHeight/h);
// bitmap is the resized bitmap
Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);
is=null;
return bitmap;
}
write your method like this..and you are done.
It is possible that your psignature1, psignature2, psignature3
is defined by the same id
Maybe your code is like this
psignature1 = (ImageButton) findViewById(R.id.psignature1);
psignature2 = (ImageButton) findViewById(R.id.psignature1);
psignature3 = (ImageButton) findViewById(R.id.psignature1);
Try changing each id
Here's my bitmap compressor for images in sd card,
just edit it
public Bitmap compressBitmapResource(String filePath, int reqWidth,
int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateBitmapSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inPurgeable = true;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
public int calculateBitmapSize(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);
}
}
return inSampleSize;
}