I'm trying to scale a bitmap to screen size and set it as wallpaper, but I've noticed on some devices(where the screen is a bit wider) the wallpaper is a little messy in height. Can someone help me scale the image in right way?
Here is my code:
int widthPx = getWindowManager().getDefaultDisplay()
.getWidth();
int heightPx = getWindowManager().getDefaultDisplay()
.getHeight();
// bmp = Bitmap.createScaledBitmap(bmp, widthPx, heightPx,
// true);
BitmapDrawable drawable = (BitmapDrawable) background
.getDrawable();
Bitmap bitmap = drawable.getBitmap();
try {
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
bitmap,
myWallpaperManager.getDesiredMinimumWidth(),
myWallpaperManager.getDesiredMinimumHeight(),
true);
myWallpaperManager.setWallpaperOffsetSteps(1, 1);
myWallpaperManager.suggestDesiredDimensions(widthPx,
heightPx);
myWallpaperManager.setBitmap(resizedBitmap);
Toast.makeText(BackgroundPreview.this,
getString(R.string.wallpaper_set),
Toast.LENGTH_SHORT).show();
finish();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(BackgroundPreview.this,
getString(R.string.error_setting_wallpaper),
Toast.LENGTH_SHORT).show();
}
Related
How to capture shadow or elevation of views in the layout of the activity in a screenshots.This code take a screenshot for the view but it's not showing the shadow of the viewsenter image description here
View screenView = parentMain;
screenView.buildDrawingCache();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getWidth() , screenView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
screenView.layout(0, 0, screenView.getLayoutParams().width, screenView.getLayoutParams().height);
screenView.draw(c);
screenView.setDrawingCacheEnabled(false);
fakeImgView.setImageBitmap(bitmap);
Even if we add the harware acceleration at activity level it does not provides any effect.
Appreciate any alternative approaches
the result
Try this.
CardView card = (CardView) findViewById(R.id.card);
Now just pass the card to captureScreenShot(). It returns the bitmap and save that bitmap saveImage().
You can pass any view Like RelativeLayout, LinearLayout etc any view can pass to captureScreenShot().
// Function which capture Screenshot
public Bitmap captureScreenShot(View view) {
/*
* Creating a Bitmap of view with ARGB_4444.
* */
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
Drawable backgroundDrawable = view.getBackground();
if (backgroundDrawable != null) {
backgroundDrawable.draw(canvas);
} else {
canvas.drawColor(Color.parseColor("#80000000"));
}
view.draw(canvas);
return bitmap;
}
// Function which Save image.
private void saveImage(Bitmap bitmap) {
File file = // Your Storage directory name + your filename
if (file == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Finally call this function like this.
saveImage(captureScreenShot(card));
Now set Your Image like this.
File file = new File(“yourImageFilePath”);
if(file.exists())
{
yourImageView.setImageURI(Uri.fromFile(file));
}
Note : If setImageURI() not working then you can use below code.
File file = new File(“yourImageFilePath”);
if(file.exists())
{
Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
yourImageView.setImageBitmap(bitmap);
}
I am using Camera intent to capture a photo on android, when intent from onActivityResult returns bitmap it has wrong orientation on some phones.
I know there are ways to fix this,but all the solutions I have seen talk about image stored in file.
What I am retrieving from intent is directly bitmap image. I want to know how I can get exif data of a bitmap and then correct its orientation. I repeat I have seen answers which deal with file and not bitmap, so please consider this before down voting.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, Constants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
And result is as follows
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
How to get orientation and rotate it.
UPDATE
Exif is a file format that inserts some information data to JPEG.
https://www.media.mit.edu/pia/Research/deepview/exif.html
And Bitmap is data structior data holds row pixel data, no exif info.
So I think it is impossible to get exif info from Bitmap.
There is no method to get exif info.
https://developer.android.com/reference/android/graphics/Bitmap.html
ORIGINAL
I agree with #DzMobNadjib .
I think the info of rotation is only in exif.
To take exif, I recommend you to take following steps.
1. Start camera activity with file path.
See [Save the Full-size Photo] capture of this document.
You can start the camera activity with file path.The camera activity will save the image to the file path that you passed.
2. In 'onActivityResult', Follow this answer (as #DzMobNadjib suggested)
Your code will be like this:
(Sorry I'm not tested. Please read carefuly and follow the above answer)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == Constants.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
Uri uri = data.getData();
Bitmap bitmap = getAdjustedBitmap(uri);
}
}
}
private Bitmap getAdjustedBitmap(Uri uri) {
FileInputStream is = null;
try {
ExifInterface exif = new ExifInterface(uri.getPath());
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
Matrix matrix = new Matrix();
if (rotation != 0f) {
matrix.preRotate(rotationInDegrees);
}
is = new FileInputStream(new File(uri.getPath()));
Bitmap sourceBitmap = BitmapFactory.decodeStream(is);
int width = sourceBitmap.getWidth();
int height = sourceBitmap.getHeight();
return Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
return null;
}
private static int exifToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { return 180; }
else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { return 270; }
return 0;
}
in the end I'm starting up a service that I will want to call to that class and change my phone's wallpaper.
in the mainActivity java file I can just write >
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap bmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.superman);
Bitmap bitmap = Bitmap.createScaledBitmap(bmap2, width, height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.superman);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
and this works great... it uploads the wallpaper and starches it to fit screen
but I can't use it in another java class.,
how do I do this on another java class, and then later on call it from my myService Class ?
maybe don't use getWindowManager, from outside your activity ? since you can't use it outside activity class
I'm using the picasso library for my app and it works very well so far. I have two activities, one which displays all the images and one that displays one image when I clicked on it in the first activity. The image is loaded into an imageView.
Now I want to set the content of this imageView as my homescreen wallpaper. So far I have this:
if (id == R.id.set_wall) {
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.id.image);
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(DetailActivity.this, "Wallpaper set",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(DetailActivity.this,
"Error setting wallpaper", Toast.LENGTH_SHORT)
.show();
}
return true;
}
But this gives me this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
Does anyone have an idea how I can set the content of this imageView as my wallpaper?
Thank you a lot!!
replace Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.id.image);
by Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
if you do not have a Drawable then ,
code to get bitmap from imageview
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
I have one app that like a Slide show or gallery but i want when anybody use this app be able to set the image to background on android phone
the app can show image i just want to able set image to background - and its can be enough if i can set address of image in Variable and then i can use this Code
WallpaperManager wpm = WallpaperManager.getInstance(getApplicationContext());
try {
wpm.setResource(R.drawable.image_1);
} catch (IOException e) {
e.printStackTrace();
}
Here is a simple tutorial for it. visit this link And if you have imagePath then use following code as :-
is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(
bitmap, parent.getWidth(), parent.getHeight(), true);
bitmap.recycle();
if(imagePath!=null){
wallpaperManager = WallpaperManager.getInstance(this);
wallpaperDrawable = wallpaperManager.getDrawable();
wallpaperManager.setBitmap(useThisBitmap);
}