How to get notification icon of other app? - android

I'm using NotificationListenerService for catching notification, with the help of kpbird blog. But I'm unable to extract the icon's drawable. I'm also going through this, but things are not cleared to me. Please help.

To get other application icon, just get package name of that application and use below code. You will get package name from notification instance.
String pack= "com.whatsapp" // ex. for whatsapp;
Context remotePackageContext = null;
Bitmap bmp = null;
try {
remotePackageContext = getApplicationContext().createPackageContext(pack, 0);
Drawable icon = remotePackageContext.getResources().getDrawable(id);
if(icon !=null) {
bmp = ((BitmapDrawable) icon).getBitmap();
}
} catch (Exception e) {
e.printStackTrace();
}

Related

How to pass the dynamic path of a drawable to BitmapFactory.decoreResource()'s 2nd parameter

I'm trying to implement Bitmap memeCanvas = BitmapFactory.decodeResource(getResources(), xxx).copy(Bitmap.Config.ARGB_8888, true); to draw on the images that I upload from my phone to my ImageView. Because I don't have a specific drawable path like R.drawable.hypotheticalImage, I don't know how to pass the same information for a drawable path that is dynamic as the 2nd parameter of BitmapFactory.decodeResource().
I can supply code per request.
This is my solution. I had to call getContext() to be able to access getContentResolver() and wrap the entire thing in a try-catch block.
Bitmap memeCanvas = null;
try {
memeCanvas = BitmapFactory
.decodeStream(getContext()
.getContentResolver()
.openInputStream(imageUri))
.copy(Bitmap.Config.ARGB_8888, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

Android - How to get current wallpaper name

I'm developing an application that sets wallpapers from com.android.launcher3 package drawable resources. At some point I need to check if the wallpaper is set correctly so I can move on to other step.
After some research in SO and googling, I wasn't able to find any information about getting current wallpaper name.
Here is how I set the drawable which I have no problem:
try {
WallpaperManager wallpaper_manager = WallpaperManager.getInstance(m_context);
Resources res = m_context.getPackageManager().getResourcesForApplication("com.android.launcher3");
int drawable_id = res.getIdentifier(wallpaper_name, "drawable", "com.android.launcher3");
Drawable drawable = res.getDrawable(drawable_id, null);
if(drawable != null) {
wallpaper_manager.setBitmap(((BitmapDrawable)drawable).getBitmap());
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I can get the current wallpaper as drawable as well:
WallpaperManager wallpaper_manager = WallpaperManager.getInstance(m_context);
Drawable drawable = wallpaper_manager.getDrawable();
but I haven't managed to get current wallpaper name.
I need help.
Thanks in advance.
I would try this two options:
1. Using wallpaperManager
You shouold be able to get the information using this:
wallpaperManager.getWallpaperInfo();
This will return a WallpaperInfo object which contains all the data about the wallpaper.
More information
https://developer.android.com/reference/android/app/WallpaperManager.html
2. Getting the drawable file
You can also try to get the URI of the drawable like this:
String imageUri = "drawable://" + R.drawable.image;
And get the file name from there.
Hope it helps you.

Getting images from a string path to be viewed in an imageview

I have some picture paths stored in a datastore and i am trying to convert them into drawables and display the in my image view, for some reason im getting a null pointer exception. Can someone please help me? Thanks
String pathName = selectedPlayer.getPicture();
Toast.makeText(this, pathName, Toast.LENGTH_SHORT).show();
Drawable d = Drawable.createFromPath(pathName);
imageView.setImageDrawable(d);
You must check file name is not null
then check that first if file exists or not
if(pathName!=null && pathName!="") <--CHECK FILENAME IS NOT NULL
{
File f = new File(pathName);
if(f.exists()) <-- CHECK FILE EXISTS OR NOT
{
Drawable d = Drawable.createFromPath(pathName);
imageView.setImageDrawable(d);
}
}
EDIT :
You have to initialize your imageview first like
imageView=(ImageView)findViewById(R.id.yourimageviewid);
Look at imageView it should not be null.
And after that try this :
bm = null;
try {
bm = BitmapFactory.decodeFile(imageView);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
imageView.setImageBitmap(bm);

creating a drawable from sd card to set as a background in android

I am trying to use an image from the sd card and set it as the background for a relativelayout. I have tried other solutions that i have found here and elsewhere but they havent seemed to work for me. here is my code. I have commented out other ways that i have tried and didnt work. the only thing that worked for me was using setBackgroudnResource and using a resource from the app, but this was just to test to make sure mRoot was set up correctly. when I have tried all the other ways, it just doesn't set anything. Anyone know what I am doing wrong, or if there is a better way to do this?
//one way i tired...
//String extDir = Environment.getExternalStorageDirectory().toString();
//Drawable d = Drawable.createFromPath(extDir + "/pic.png");
//mRoot.setBackgroundDrawable(d);
//another way tried..
//Drawable d = Drawable.createFromPath("/sdcard/pic.png");
//mRoot.setBackgroundDrawable(d);
//last way i tried...
mRoot.setBackgroundDrawable(Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.png").getAbsolutePath()));
//worked, only to verify mRoot was setup correctly and it could be changed
//mRoot.setBackgroundResource(R.drawable.bkg);
You do not load a drawable from SD card but a bitmap. Here is a method to load it with the reduced sampling (quality) so the program will not complain if the image is too large. Then I guess you need to process this bitmap i.e. crop it and resize for the background.
// Read bitmap from Uri
public Bitmap readBitmap(Uri selectedImage) {
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //reduce quality
AssetFileDescriptor fileDescriptor =null;
try {
fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,"r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
try {
bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
fileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bm;
}
The Uri here can be supplied from a gallery picker activity.
The image then can be saved into application resources and loaded into an imageView
private void saveBackground(Bitmap Background) {
String strBackgroundFilename = "background_custom.jpg";
try {
Background.compress(CompressFormat.JPEG, 80, openFileOutput(strBackgroundFilename, MODE_PRIVATE));
} catch (Exception e) {
Log.e(DEBUG_TAG, "Background compression and save failed.", e);
}
Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(BackgroundSettings.this.getFilesDir(), strBackgroundFilename));
// Load this image
Bitmap bitmapImage = BitmapFactory.decodeFile(imageUriToSaveCameraImageTo.getPath());
Drawable bgrImage = new BitmapDrawable(bitmapImage);
//show it in a view
ImageView backgroundView = (ImageView) findViewById(R.id.BackgroundImageView);
backgroundView.setImageURI(null);
backgroundView.setImageDrawable(bgrImage);
}
File file = new File( url.getAbsolutePath(), imageUrl);
if (file.exists()) {
mDrawable = Drawable.createFromPath(file.getAbsolutePath());
}
I suggest checking that the drawable is being loaded correctly. Some things to try:
Try using a different image on the sd card
Put pic.png in R.drawable and make sure mRoot.setBackgroundResource() does what you expect
After loading the drawable, check d.getBounds() to make sure it is what you expect

Android - how to set the wallpaper image

Is it possible to set the android wallpaper image programatically? I'd like to create a service that downloads an image from the web and updates the home screen wallpaper periodically.
If you have image URL then use
WallpaperManager wpm = WallpaperManager.getInstance(context);
InputStream ins = new URL("absolute/path/of/image").openStream();
wpm.setStream(ins);
If you have image URI then use
WallpaperManager wpm = WallpaperManager.getInstance(context);
wpm.setResource(Uri.of.image);
In your manifest file:
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
From this page on the developer site:
public void setStream (InputStream data)
Change the current system wallpaper to a specific byte stream. The give InputStream is copied into persistent storage and will now be used as the wallpaper. Currently it must be either a JPEG or PNG image.
If you have bitmap of image than you will add this function to set as wallpaper:
public void SetBackground(int Url) {
try {
File file = new File("/sdcard/sampleimage");
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Url);
bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(file));
Context context = this.getBaseContext();
context.setWallpaper(bitmap);
Toast.makeText(getApplicationContext(), "Wallpaper has been set", Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
you should add permission for this
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
hope it will work
OK Here's how to do it before api 2.0:
You need to call getApplicationContext.setWallpaper() and pass it the bitmap.
This method is now deprecated. See ChrisF's answer for details on the new method.

Categories

Resources