I need to get the android-resource
uri (of the form android.resource://[package]/[res id]
) of an application icon (of any given package). This is passed to another application to set an ImageView using the setImageUri
API. However, the image being set is a low resolution version and I would like to know how to get the Uri for the high resolution version.
As a test code, I have 3 ImageViews. The first two, I am setting the image using setDrawable
. The third using setImageUri
for the same application icon.
package com.test.icontest
import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.DisplayMetrics
import android.widget.ImageView
class MainActivity : AppCompatActivity() {
companion object {
const val TEST_PACKAGE = "com.washingtonpost.rainbow"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imageView = findViewById<ImageView>(R.id.iconImage)
imageView.setImageDrawable(getAppIconDrawable(applicationContext, false))
val imageView2 = findViewById<ImageView>(R.id.iconImage2)
imageView2.setImageDrawable(getAppIconDrawable(applicationContext))
val imageView3 = findViewById<ImageView>(R.id.iconImage3)
imageView3.setImageURI(getAppIconUrl(applicationContext))
}
// Returns the Uri for the android application icon
fun getAppIconUrl(context: Context): Uri {
val iconCode = context.packageManager.getApplicationInfo(TEST_PACKAGE, 0).icon
// Third image below
return Uri.Builder()
.scheme("android.resource")
.authority(TEST_PACKAGE)
.path(iconCode.toString())
.build()
}
// Returns the Drawable for the android application icon
fun getAppIconDrawable(context: Context, highRes: Boolean = true): Drawable {
val applicationInfo = context.packageManager.getApplicationInfo(TEST_PACKAGE, 0)
val res = context.packageManager.getResourcesForApplication(applicationInfo)
return if (highRes)
res.getDrawableForDensity(applicationInfo.icon, DisplayMetrics.DENSITY_XXXHIGH, null) // Second image below
else
res.getDrawable(applicationInfo.icon, null) // First image below
}
}
The result:
First image - set using setDrawable
- kinda blurry
Second image - set using setDrawable
with DENSITY_XXXHIGH
Third image - set using setImageUri
- kinda blurry, but I want this "sharper" like the second image above
from Getting high resolution android-resource Uri for Android application icon
No comments:
Post a Comment