How to open set as wallpaper intent in android? - java

I am trying to open a default intent of android gallery (screenshot given below).
for this i have referred some
SO Questions but none of them are worked for me Question and this Question
here's a screenshot what i want...samsung device's gallery
this is a working app on playstore..wallpaperApp
can Anyone please tell me how can i achieve this?
Thanks In Advance!!

You can use wallpaper manager to set image as wallpaper.
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.wallpaper);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});

Related

Android Studio button material stop activity when icon change

I am new to android, I have a MaterialButton and I would like its icon to change, but the activity suddenly stops, I do not see where the problem comes from, I show you the code, it is an audio player. (if you need all code of class I send u, inform me)
xml code :
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_play_music"
style="#style/myTheme.btn_last_music"
android:layout_marginLeft="#dimen/dim_10"
android:layout_marginRight="#dimen/dim_10"
app:icon="#drawable/ic_baseline_play_arrow_24"
tools:ignore="SpeakableTextPresentCheck"
/>
onCreate :
btnPlay = (MaterialButton) findViewById(R.id.btn_play_music);
btnPlay.setOnClickListener(this);
btnPlay.setTag(5);
method to play music :
public void playIfItemClicked(SearchMusic lm, int i) {
mediaPlayer.reset();
Uri u = lm.getListMusic().get(i).getUri();
InteractiveMusic intMus = new InteractiveMusic(mediaPlayer, u);
try {
intMus.playMusic();
btnPlay.setIconTintResource(R.drawable.ic_pause);
} catch (IOException e) {
e.printStackTrace();
}
}
ok, i have find the solution,
it's this line :
btnPlay.setIconTintResource(R.drawable.ic_pause);
change to :
btnPlay.setIconResource(R.drawable.ic_pause);

How to load image from URL in Fragment class?

I want to load an image from its URL in Fragment class. I'm loading the image like this:
try {
// ImageView j=(ImageView)rootView.findViewById(R.id.image_frm);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(image_url).getContent());
Bitmap resized = Bitmap.createScaledBitmap(bitmap, 600, 600, true);
Bitmap conv_bm = getRoundedRectBitmap(resized, 255);
i.setImageBitmap(conv_bm);
// j.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
but this method is taking more time to load the image and to open it.
If I want to use ImageLoader class like this tutorial, then how to load it in Fragment?
Use any 3rd party library like Picasso. Download the .jar file and put the it inside your libs folder. For more info see Picasso's documentation.
in case of Fragments, use getActivity() instead of context.
For rounded ImageView, I use the RoundedImageView lib.

Android MediaPlayer plays song twice

I'm making a simple splash screen so that when an app loads it shows a small logo and plays a little jingle.
I've set it up as so:
splashSong = MediaPlayer.create(MainActivity.this, R.raw.splash);
splashSong.start();
Thread splashThread = new Thread(){
public void run(){
try{
sleep(6000);
}
}catch (InterruptedException e){
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
Intent openMenu = new Intent("com.mmm.MAINMENU");
startActivity(openMenu);
}
}
};
splashThread.start();
However there is an issue with the play count. When it loads the splash class, the jingle is played twice. I've changed a series of things such as preparing the song and setting the datasource. This is, however, not successful and the screen still plays the song twice.
Anybody have any ideas as to why it might be doing this?
Thanks,
Add
splashSong.setLooping(false);
Thanks to Mr. Me for the help. It was to do with the applications orientation. Removed that and all worked!

Launch crop image activity before setting wallpaper

In my application users can click a button to set an image from imageview as wallpaper.
Here is the code:
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(
((BitmapDrawable) fullSizeImage.getDrawable()).getBitmap());
//setResource(fullSizeImage.getDrawable());
Toast.makeText(
FullSizeImageDisplay.this,
"Wallpaper set",Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
It is setting the image as wallpaper, but not working properly on all devices, where only a part of the picture is set as wallpaper.
I want user to get an option to crop the image before setting it as wallpaper, like Android shows before setting an image as wallpaper from Gallery.
Can that activity from Gallery be launched from my app to crop image or is there another alternative?
You could start an intent in onActivityResult after loading Image. There You could start the cropping with this intent, for example:
Intent cropYourPicIntent = new Intent(
"com.android.camera.action.CROP");
But this is only to get the idea of it what I mean, there is a lot of more code to do.

How to set android lock screen image

I'm just getting started with android programming, and want to see if there is a way to programmatically set the lock screen image. I've found various ways of setting the wallpaper in the API, but I can't seem to find the equivalent ways of setting the lock screen image.
I've seen various posts saying that customising the lock screen by adding widgets or bits of applications is not possible, but surely there must be a way to set the image programmatically?
Cheers,
Robin
As of API Level 24 they have added new methods (and updated the documentation) and flags to the WallpaperManager which allow you to set a Wallpaper not only to the home screen but also to the Lockscreen
To set a Wallpaper to the Lockscreen use the new flag WallpaperManager.FLAG_LOCK, and one of the methods which take int which
WallpaperManager.getInstance(this).setStream(inputStream, null, true, WallpaperManager.FLAG_LOCK);
You can also use one of the following methods
int setStream (InputStream bitmapData, Rect visibleCropHint, boolean allowBackup, int which)
int setResource (int resid, int which)
int setBitmap (Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, int which)
A nice addition is that you can now also check if you are allowed to set the wallpaper via isSetWallpaperAllowed, and get the current set wallpaper via getWallpaperFile
Check out the updated documentation for the WallpaperManager.
There is no "lock screen image" in Android. There most certainly is no "lock screen image" concept that is the same between stock Android, HTC Sense, MOTOBLUR, etc. This simply is not part of the Android SDK.
The project that Mr. Rijk points to is a security violation that pretends to be a lock screen replacement.
There is a way to do it on Samsung devices. In the intent you can put an extra.
intent.putExtra("SET_LOCKSCREEN_WALLPAPER", true);
startActivity(intent);
I've only tested this on some Samsung phones and there's no guarantee that this won't break some time in the future. Use with caution.
You can use these three methods of WalpaperManager class but it will only work for nought version devices or above it:-
public int setBitmap (Bitmap fullImage,
Rect visibleCropHint,
boolean allowBackup,
int which)
public int setResource (int resid,
int which)
public int setStream (InputStream inputStreamData,
Rect visibleCropHint,
boolean allowBackup,
int which)
Parameter of these three methods:-
Bitmap/resid/inputStreamData :-this parameter accept data
visibleCropHint:-this parameter accept Rect object which is mainly used for Cropping functionality, for more information refer to Android developer reference website, you can also pass null if u don't want cropping functionality
allowBackup:-boolean: true if the OS is permitted to back up this wallpaper image for restore to a future device; false otherwise.
which:-It is one of the most important parameter which helps you to configure wallpaper for lock screen and home wallpaper. for lock screen use WalpaperManager.FLAG_LOCK and for home wallpaper use FLAG_SYSTEM
I am giving one example to make you understand how to use it:-
WalaperManager wm = WalaperManager.getInstance();
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
wm.setBitmap(bitmap,null,true,WalpaperManager.FLAG_LOCK);//For Lock screen
Toast.makeText(context.context, "done", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context.context, "Lock screen walpaper not supported",
Toast.LENGTH_SHORT).show();
}
} catch (e: Exception) {
Toast.makeText(context.context, e.message, Toast.LENGTH_SHORT).show();
}
for more information visit Android developer wallpaper manager reference
There is another way to do this. at first ,you need save the pic which you wanna set in lockedscreen in a folder(suppose it's called "appName").and then ,use following code to open gallery, after gallery has opened.lead user to open "appName" folder ,and choose the pic in gallery of system. in the gallery,user can set a pic as wallpaper or lockscreen paper.
// this code to open gallery.
startActivity(new Intent(Intent.ACTION_SET_WALLPAPER));
Bitmap icon = BitmapFactory.decodeResource(getViewContext().getResources(), R.drawable.wall);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getViewContext());
try {
wallpaperManager.setBitmap(icon, null, true, WallpaperManager.FLAG_LOCK);
} catch (IOException e) {
e.printStackTrace();
}
usage for api30+
public void onWallpaperChanged(Bitmap bitmap, boolean onHomeScreen, boolean onLockScreen) {
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
if(onHomeScreen) {
myWallpaperManager.setBitmap(bitmap);// For Home screen
}
if(onLockScreen) {
myWallpaperManager.setBitmap(bitmap,null,true, WallpaperManager.FLAG_LOCK);//For Lock screen
}
} catch (IOException e) {
e.printStackTrace();
}
}
Since API level 24, you can set wallpaper to your home screen, lock screen, or both:
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// home screen
wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_SYSTEM);
// lock screen
wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_LOCK);
// home screen & lock screen
wallpaperManager.setBitmap(mBitmap, null, true, WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM);
} else {
wallpaperManager.setBitmap(mBitmap);
}
source

Categories