Java Code Examples for android.content.pm.PackageManager#getResourcesForApplication()
The following examples show how to use
android.content.pm.PackageManager#getResourcesForApplication() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomTypefaceManager.java From zom-android-matrix with Apache License 2.0 | 7 votes |
public static void loadFromKeyboard (Context context) { PackageManager packageManager = context.getPackageManager(); String fontName = "DDC_Uchen.ttf"; try { Resources res = packageManager.getResourcesForApplication("org.ironrabbit.bhoboard"); InputStream reader = res.getAssets().open(fontName); File fileFont = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),fontName); OutputStream writer = new FileOutputStream(fileFont); byte[] buffer = new byte[32000]; int l = 0; while((l = reader.read(buffer)) > 0) { writer.write(buffer, 0, l); } writer.close(); mTypeface = Typeface.createFromFile(fileFont); } catch (Exception e) { Log.e("CustomTypeface","can't find assets",e); } }
Example 2
Source File: Utilities.java From LB-Launcher with Apache License 2.0 | 6 votes |
/** * Returns a bitmap suitable for the all apps view. If the package or the resource do not * exist, it returns null. */ static Bitmap createIconBitmap(String packageName, String resourceName, IconCache cache, Context context) { PackageManager packageManager = context.getPackageManager(); // the resource try { Resources resources = packageManager.getResourcesForApplication(packageName); if (resources != null) { final int id = resources.getIdentifier(resourceName, null, null); return createIconBitmap( resources.getDrawableForDensity(id, cache.getFullResIconDpi()), context); } } catch (Exception e) { // Icon not found. } return null; }
Example 3
Source File: Utilities.java From Trebuchet with GNU General Public License v3.0 | 6 votes |
/** * Returns a bitmap suitable for the all apps view. If the package or the resource do not * exist, it returns null. */ public static Bitmap createIconBitmap(String packageName, String resourceName, Context context) { PackageManager packageManager = context.getPackageManager(); // the resource try { Resources resources = packageManager.getResourcesForApplication(packageName); if (resources != null) { final int id = resources.getIdentifier(resourceName, null, null); return createIconBitmap( resources.getDrawableForDensity(id, LauncherAppState.getInstance() .getInvariantDeviceProfile().fillResIconDpi), context); } } catch (Exception e) { // Icon not found. } return null; }
Example 4
Source File: Utilities.java From Trebuchet with GNU General Public License v3.0 | 6 votes |
static Pair<String, Resources> findSystemApk(String action, PackageManager pm) { final Intent intent = new Intent(action); for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) { if (info.activityInfo != null && (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { final String packageName = info.activityInfo.packageName; try { final Resources res = pm.getResourcesForApplication(packageName); return Pair.create(packageName, res); } catch (NameNotFoundException e) { Log.w(TAG, "Failed to find resources for " + packageName); } } } return null; }
Example 5
Source File: CustomTypefaceManager.java From Zom-Android-XMPP with GNU General Public License v3.0 | 6 votes |
public static void loadFromKeyboard (Context context) { PackageManager packageManager = context.getPackageManager(); String fontName = "DDC_Uchen.ttf"; try { Resources res = packageManager.getResourcesForApplication("org.ironrabbit.bhoboard"); InputStream reader = res.getAssets().open(fontName); File fileFont = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),fontName); OutputStream writer = new FileOutputStream(fileFont); byte[] buffer = new byte[32000]; int l = 0; while((l = reader.read(buffer)) > 0) { writer.write(buffer, 0, l); } writer.close(); mTypeface = Typeface.createFromFile(fileFont); } catch (Exception e) { Log.e("CustomTypeface","can't find assets",e); } }
Example 6
Source File: EmojiManager.java From Yahala-Messenger with MIT License | 6 votes |
public void addJsonPlugins() throws IOException, JsonSyntaxException { PackageManager packageManager = mContext.getPackageManager(); Intent stickerIntent = new Intent(PLUGIN_CONSTANT); List<ResolveInfo> stickerPack = packageManager.queryIntentActivities(stickerIntent, 0); for (ResolveInfo ri : stickerPack) { try { Resources res = packageManager.getResourcesForApplication(ri.activityInfo.applicationInfo); String[] files = res.getAssets().list(""); for (String file : files) { if (file.endsWith(".json")) addJsonDefinitions(file, file.substring(0, file.length() - 5), "png", res); } } catch (NameNotFoundException e) { Log.e("emoji", "unable to find application for emoji plugin"); } } }
Example 7
Source File: UserGuideActivity.java From prevent with Do What The F*ck You Want To Public License | 5 votes |
private CharSequence getLabel(PackageManager pm, ApplicationInfo info) throws PackageManager.NameNotFoundException { CharSequence label = null; if ("com.android.vending".equals(info.packageName)) { Resources resources = pm.getResourcesForApplication(info); int appName = resources.getIdentifier("app_name", "string", info.packageName); if (appName > 0) { label = resources.getText(appName); } } if (TextUtils.isEmpty(label)) { label = pm.getApplicationLabel(info); } return label; }
Example 8
Source File: InstallApkUtil.java From EdXposedManager with GNU General Public License v3.0 | 5 votes |
public static String getAppLabel(ApplicationInfo info, PackageManager pm) { try { if (info.labelRes > 0) { Resources res = pm.getResourcesForApplication(info); Configuration config = new Configuration(); config.setLocale(Locale.getDefault()); res.updateConfiguration(config, res.getDisplayMetrics()); return res.getString(info.labelRes); } } catch (Exception ignored) { } return info.loadLabel(pm).toString(); }
Example 9
Source File: WebApkInfo.java From 365browser with Apache License 2.0 | 5 votes |
/** Decodes bitmap from WebAPK's resources. */ private static Bitmap decodeImageResource(String webApkPackageName, int resourceId) { PackageManager packageManager = ContextUtils.getApplicationContext().getPackageManager(); try { Resources resources = packageManager.getResourcesForApplication(webApkPackageName); return BitmapFactory.decodeResource(resources, resourceId); } catch (PackageManager.NameNotFoundException e) { return null; } }
Example 10
Source File: PluginManager.java From hackerskeyboard with Apache License 2.0 | 5 votes |
Resources getResources(Context context) { PackageManager packageManager = context.getPackageManager(); Resources res = null; try { ApplicationInfo appInfo = packageManager.getApplicationInfo(mPackageName, 0); res = packageManager.getResourcesForApplication(appInfo); } catch (NameNotFoundException e) { Log.i(TAG, "couldn't get resources"); } return res; }
Example 11
Source File: SkiaImageRegionDecoder.java From BlogDemo with Apache License 2.0 | 4 votes |
@Override @NonNull public Point init(Context context, @NonNull Uri uri) throws Exception { String uriString = uri.toString(); if (uriString.startsWith(RESOURCE_PREFIX)) { Resources res; String packageName = uri.getAuthority(); if (context.getPackageName().equals(packageName)) { res = context.getResources(); } else { PackageManager pm = context.getPackageManager(); res = pm.getResourcesForApplication(packageName); } int id = 0; List<String> segments = uri.getPathSegments(); int size = segments.size(); if (size == 2 && segments.get(0).equals("drawable")) { String resName = segments.get(1); id = res.getIdentifier(resName, "drawable", packageName); } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) { try { id = Integer.parseInt(segments.get(0)); } catch (NumberFormatException ignored) { } } decoder = BitmapRegionDecoder.newInstance(context.getResources().openRawResource(id), false); } else if (uriString.startsWith(ASSET_PREFIX)) { String assetName = uriString.substring(ASSET_PREFIX.length()); decoder = BitmapRegionDecoder.newInstance(context.getAssets().open(assetName, AssetManager.ACCESS_RANDOM), false); } else if (uriString.startsWith(FILE_PREFIX)) { decoder = BitmapRegionDecoder.newInstance(uriString.substring(FILE_PREFIX.length()), false); } else { InputStream inputStream = null; try { ContentResolver contentResolver = context.getContentResolver(); inputStream = contentResolver.openInputStream(uri); decoder = BitmapRegionDecoder.newInstance(inputStream, false); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { /* Ignore */ } } } } return new Point(decoder.getWidth(), decoder.getHeight()); }
Example 12
Source File: SkiaImageDecoder.java From PictureSelector with Apache License 2.0 | 4 votes |
@Override public Bitmap decode(Context context, Uri uri) throws Exception { String uriString = uri.toString(); BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap; options.inPreferredConfig = Bitmap.Config.RGB_565; if (uriString.startsWith(RESOURCE_PREFIX)) { Resources res; String packageName = uri.getAuthority(); if (context.getPackageName().equals(packageName)) { res = context.getResources(); } else { PackageManager pm = context.getPackageManager(); res = pm.getResourcesForApplication(packageName); } int id = 0; List<String> segments = uri.getPathSegments(); int size = segments.size(); if (size == 2 && segments.get(0).equals("drawable")) { String resName = segments.get(1); id = res.getIdentifier(resName, "drawable", packageName); } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) { try { id = Integer.parseInt(segments.get(0)); } catch (NumberFormatException ignored) { } } bitmap = BitmapFactory.decodeResource(context.getResources(), id, options); } else if (uriString.startsWith(ASSET_PREFIX)) { String assetName = uriString.substring(ASSET_PREFIX.length()); bitmap = BitmapFactory.decodeStream(context.getAssets().open(assetName), null, options); } else if (uriString.startsWith(FILE_PREFIX)) { bitmap = BitmapFactory.decodeFile(uriString.substring(FILE_PREFIX.length()), options); } else { InputStream inputStream = null; try { ContentResolver contentResolver = context.getContentResolver(); inputStream = contentResolver.openInputStream(uri); bitmap = BitmapFactory.decodeStream(inputStream, null, options); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { } } } } if (bitmap == null) { throw new RuntimeException("Skia image region decoder returned null bitmap - image format may not be supported"); } return bitmap; }
Example 13
Source File: IconPackManager.java From emerald with GNU General Public License v3.0 | 4 votes |
public void setIcons() { iconsData = new HashMap<String, String>(); iconBacks = null; iconMask = null; iconUpon = null; factor = 1.0f; iconPackRes = null; if ("default".equals(iconPackName)) { return; } transformDrawable = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Keys.TRANSFORM_DRAWABLE, true); String component = null; String drawable = null; PackageManager pm = context.getPackageManager(); iconBacks = new ArrayList<Bitmap>(); try { iconPackRes = pm.getResourcesForApplication(iconPackName); } catch (PackageManager.NameNotFoundException nameNotFound) { } try { int id = iconPackRes.getIdentifier("appfilter", "xml", iconPackName); XmlPullParser parser = iconPackRes.getXml(id); int parserEvent = parser.getEventType(); while (parserEvent != XmlPullParser.END_DOCUMENT) { if (parserEvent == XmlPullParser.START_TAG){ final String parserName = parser.getName(); if ("item".equals(parserName)) { for (int i = 0; i < parser.getAttributeCount(); i++) { if ("component".equals(parser.getAttributeName(i))) { component = parser.getAttributeValue(i); int c = component.indexOf("{"); component = component.substring(c+1, component.length()-1); } else if ("drawable".equals(parser.getAttributeName(i))) { drawable = parser.getAttributeValue(i); } } iconsData.put(component, drawable); } else if ("iconback".equals(parserName)) { for (int i = 0; i < parser.getAttributeCount(); i++) { iconBacks.add(loadBitmap(parser.getAttributeValue(i))); } } else if ("iconmask".equals(parserName) && parser.getAttributeCount() > 0 && "iconmask".equals(parser.getAttributeName(0))) { iconMask = loadBitmap(parser.getAttributeValue(0)); } else if ("iconupon".equals(parserName) && parser.getAttributeCount() > 0 && "iconupon".equals(parser.getAttributeName(0))) { iconUpon = loadBitmap(parser.getAttributeValue(0)); } else if ("scale".equals(parserName) && "factor".equals(parser.getAttributeName(0))) { factor = Float.valueOf(parser.getAttributeValue(0)); } } parserEvent = parser.next(); } } catch (Exception e) { } }
Example 14
Source File: SkiaImageDecoder.java From Matisse-Kotlin with Apache License 2.0 | 4 votes |
@Override public Bitmap decode(Context context, Uri uri) throws Exception { String uriString = uri.toString(); BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap; options.inPreferredConfig = Bitmap.Config.RGB_565; if (uriString.startsWith(RESOURCE_PREFIX)) { Resources res; String packageName = uri.getAuthority(); if (context.getPackageName().equals(packageName)) { res = context.getResources(); } else { PackageManager pm = context.getPackageManager(); res = pm.getResourcesForApplication(packageName); } int id = 0; List<String> segments = uri.getPathSegments(); int size = segments.size(); if (size == 2 && segments.get(0).equals("drawable")) { String resName = segments.get(1); id = res.getIdentifier(resName, "drawable", packageName); } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) { try { id = Integer.parseInt(segments.get(0)); } catch (NumberFormatException ignored) { } } bitmap = BitmapFactory.decodeResource(context.getResources(), id, options); } else if (uriString.startsWith(ASSET_PREFIX)) { String assetName = uriString.substring(ASSET_PREFIX.length()); bitmap = BitmapFactory.decodeStream(context.getAssets().open(assetName), null, options); } else if (uriString.startsWith(FILE_PREFIX)) { bitmap = BitmapFactory.decodeFile(uriString.substring(FILE_PREFIX.length()), options); } else { InputStream inputStream = null; try { ContentResolver contentResolver = context.getContentResolver(); inputStream = contentResolver.openInputStream(uri); bitmap = BitmapFactory.decodeStream(inputStream, null, options); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { } } } } if (bitmap == null) { throw new RuntimeException("Skia image region decoder returned null bitmap - image format may not be supported"); } return bitmap; }
Example 15
Source File: PkgUtil.java From NanoIconPack with Apache License 2.0 | 4 votes |
@TargetApi(17) public static String getAppLabelEn(Context context, String pkgName, String def) { if (context == null || TextUtils.isEmpty(pkgName)) { return def; } String result = def; try { PackageManager packageManager = context.getPackageManager(); ApplicationInfo applicationInfo = packageManager.getPackageInfo(pkgName, 0).applicationInfo; Configuration configuration = new Configuration(); // It's better, I think, to use Locale.ENGLISH // instead of Locale.ROOT (although I want to do). if (C.SDK >= 17) { configuration.setLocale(Locale.ENGLISH); } else { configuration.locale = Locale.ENGLISH; } // The result is a value in disorder maybe if using: // packageManager.getResourcesForApplication(PACKAGE_NAME) Resources resources = packageManager.getResourcesForApplication(applicationInfo); resources.updateConfiguration(configuration, context.getResources().getDisplayMetrics()); int labelResId = applicationInfo.labelRes; if (labelResId != 0) { // If the localized label is not added, the default is returned. // NOTICE!!!If the default were empty, Resources$NotFoundException would be called. result = resources.getString(labelResId); } /* * NOTICE!!! * We have to restore the locale. * On the one hand, * it will influence the label of Activity, etc.. * On the other hand, * the got "resources" equals the one "this.getResources()" if the current .apk file * happens to be this APK Checker(com.by_syk.apkchecker). * We need to restore the locale, or the language of APK Checker will change to English. */ if (C.SDK >= 17) { configuration.setLocale(Locale.getDefault()); } else { configuration.locale = Locale.getDefault(); } resources.updateConfiguration(configuration, context.getResources().getDisplayMetrics()); } catch (Exception e) { e.printStackTrace(); } return result; }
Example 16
Source File: SkiaImageDecoder.java From imsdk-android with MIT License | 4 votes |
@Override @NonNull public Bitmap decode(Context context, @NonNull Uri uri) throws Exception { String uriString = uri.toString(); BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap; options.inPreferredConfig = bitmapConfig; if (uriString.startsWith(RESOURCE_PREFIX)) { Resources res; String packageName = uri.getAuthority(); if (context.getPackageName().equals(packageName)) { res = context.getResources(); } else { PackageManager pm = context.getPackageManager(); res = pm.getResourcesForApplication(packageName); } int id = 0; List<String> segments = uri.getPathSegments(); int size = segments.size(); if (size == 2 && segments.get(0).equals("drawable")) { String resName = segments.get(1); id = res.getIdentifier(resName, "drawable", packageName); } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) { try { id = Integer.parseInt(segments.get(0)); } catch (NumberFormatException ignored) { } } bitmap = BitmapFactory.decodeResource(context.getResources(), id, options); } else if (uriString.startsWith(ASSET_PREFIX)) { String assetName = uriString.substring(ASSET_PREFIX.length()); bitmap = BitmapFactory.decodeStream(context.getAssets().open(assetName), null, options); } else if (uriString.startsWith(FILE_PREFIX)) { bitmap = BitmapFactory.decodeFile(uriString.substring(FILE_PREFIX.length()), options); } else { InputStream inputStream = null; try { ContentResolver contentResolver = context.getContentResolver(); inputStream = contentResolver.openInputStream(uri); bitmap = BitmapFactory.decodeStream(inputStream, null, options); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { /* Ignore */ } } } } if (bitmap == null) { throw new RuntimeException("Skia image region decoder returned null bitmap - image format may not be supported"); } return bitmap; }
Example 17
Source File: SkiaImageDecoder.java From pdfview-android with Apache License 2.0 | 4 votes |
@Override @NonNull public Bitmap decode(Context context, @NonNull Uri uri) throws Exception { String uriString = uri.toString(); BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap; options.inPreferredConfig = bitmapConfig; if (uriString.startsWith(RESOURCE_PREFIX)) { Resources res; String packageName = uri.getAuthority(); if (context.getPackageName().equals(packageName)) { res = context.getResources(); } else { PackageManager pm = context.getPackageManager(); res = pm.getResourcesForApplication(packageName); } int id = 0; List<String> segments = uri.getPathSegments(); int size = segments.size(); if (size == 2 && segments.get(0).equals("drawable")) { String resName = segments.get(1); id = res.getIdentifier(resName, "drawable", packageName); } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) { try { id = Integer.parseInt(segments.get(0)); } catch (NumberFormatException ignored) { } } bitmap = BitmapFactory.decodeResource(context.getResources(), id, options); } else if (uriString.startsWith(ASSET_PREFIX)) { String assetName = uriString.substring(ASSET_PREFIX.length()); bitmap = BitmapFactory.decodeStream(context.getAssets().open(assetName), null, options); } else if (uriString.startsWith(FILE_PREFIX)) { bitmap = BitmapFactory.decodeFile(uriString.substring(FILE_PREFIX.length()), options); } else { InputStream inputStream = null; try { ContentResolver contentResolver = context.getContentResolver(); inputStream = contentResolver.openInputStream(uri); bitmap = BitmapFactory.decodeStream(inputStream, null, options); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { /* Ignore */ } } } } if (bitmap == null) { throw new RuntimeException("Skia image region decoder returned null bitmap - image format may not be supported"); } return bitmap; }
Example 18
Source File: SkiaImageRegionDecoder.java From RxTools-master with Apache License 2.0 | 4 votes |
@Override public Point init(Context context, Uri uri) throws Exception { String uriString = uri.toString(); if (uriString.startsWith(RESOURCE_PREFIX)) { Resources res; String packageName = uri.getAuthority(); if (context.getPackageName().equals(packageName)) { res = context.getResources(); } else { PackageManager pm = context.getPackageManager(); res = pm.getResourcesForApplication(packageName); } int id = 0; List<String> segments = uri.getPathSegments(); int size = segments.size(); if (size == 2 && segments.get(0).equals("drawable")) { String resName = segments.get(1); id = res.getIdentifier(resName, "drawable", packageName); } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) { try { id = Integer.parseInt(segments.get(0)); } catch (NumberFormatException ignored) { } } decoder = BitmapRegionDecoder.newInstance(context.getResources().openRawResource(id), false); } else if (uriString.startsWith(ASSET_PREFIX)) { String assetName = uriString.substring(ASSET_PREFIX.length()); decoder = BitmapRegionDecoder.newInstance(context.getAssets().open(assetName, AssetManager.ACCESS_RANDOM), false); } else if (uriString.startsWith(FILE_PREFIX)) { decoder = BitmapRegionDecoder.newInstance(uriString.substring(FILE_PREFIX.length()), false); } else { InputStream inputStream = null; try { ContentResolver contentResolver = context.getContentResolver(); inputStream = contentResolver.openInputStream(uri); decoder = BitmapRegionDecoder.newInstance(inputStream, false); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { } } } } return new Point(decoder.getWidth(), decoder.getHeight()); }
Example 19
Source File: DeviceAdminInfo.java From android_9.0.0_r45 with Apache License 2.0 | 4 votes |
/** * Constructor. * * @param context The Context in which we are parsing the device admin. * @param activityInfo The ActivityInfo returned from the package manager about * this device admin's component. * * @hide */ public DeviceAdminInfo(Context context, ActivityInfo activityInfo) throws XmlPullParserException, IOException { mActivityInfo = activityInfo; PackageManager pm = context.getPackageManager(); XmlResourceParser parser = null; try { parser = mActivityInfo.loadXmlMetaData(pm, DeviceAdminReceiver.DEVICE_ADMIN_META_DATA); if (parser == null) { throw new XmlPullParserException("No " + DeviceAdminReceiver.DEVICE_ADMIN_META_DATA + " meta-data"); } Resources res = pm.getResourcesForApplication(mActivityInfo.applicationInfo); AttributeSet attrs = Xml.asAttributeSet(parser); int type; while ((type=parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) { } String nodeName = parser.getName(); if (!"device-admin".equals(nodeName)) { throw new XmlPullParserException( "Meta-data does not start with device-admin tag"); } TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.DeviceAdmin); mVisible = sa.getBoolean( com.android.internal.R.styleable.DeviceAdmin_visible, true); sa.recycle(); int outerDepth = parser.getDepth(); while ((type=parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } String tagName = parser.getName(); if (tagName.equals("uses-policies")) { int innerDepth = parser.getDepth(); while ((type=parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) { if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { continue; } String policyName = parser.getName(); Integer val = sKnownPolicies.get(policyName); if (val != null) { mUsesPolicies |= 1 << val.intValue(); } else { Log.w(TAG, "Unknown tag under uses-policies of " + getComponent() + ": " + policyName); } } } else if (tagName.equals("support-transfer-ownership")) { if (parser.next() != XmlPullParser.END_TAG) { throw new XmlPullParserException( "support-transfer-ownership tag must be empty."); } mSupportsTransferOwnership = true; } } } catch (NameNotFoundException e) { throw new XmlPullParserException( "Unable to create context for: " + mActivityInfo.packageName); } finally { if (parser != null) parser.close(); } }
Example 20
Source File: SkiaImageDecoder.java From EasyPhotos with Apache License 2.0 | 4 votes |
@Override @NonNull public Bitmap decode(Context context, @NonNull Uri uri) throws Exception { String uriString = uri.toString(); BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap; options.inPreferredConfig = bitmapConfig; if (uriString.startsWith(RESOURCE_PREFIX)) { Resources res; String packageName = uri.getAuthority(); if (context.getPackageName().equals(packageName)) { res = context.getResources(); } else { PackageManager pm = context.getPackageManager(); res = pm.getResourcesForApplication(packageName); } int id = 0; List<String> segments = uri.getPathSegments(); int size = segments.size(); if (size == 2 && segments.get(0).equals("drawable")) { String resName = segments.get(1); id = res.getIdentifier(resName, "drawable", packageName); } else if (size == 1 && TextUtils.isDigitsOnly(segments.get(0))) { try { id = Integer.parseInt(segments.get(0)); } catch (NumberFormatException ignored) { } } bitmap = BitmapFactory.decodeResource(context.getResources(), id, options); } else if (uriString.startsWith(ASSET_PREFIX)) { String assetName = uriString.substring(ASSET_PREFIX.length()); bitmap = BitmapFactory.decodeStream(context.getAssets().open(assetName), null, options); } else if (uriString.startsWith(FILE_PREFIX)) { bitmap = BitmapFactory.decodeFile(uriString.substring(FILE_PREFIX.length()), options); } else { InputStream inputStream = null; try { ContentResolver contentResolver = context.getContentResolver(); inputStream = contentResolver.openInputStream(uri); bitmap = BitmapFactory.decodeStream(inputStream, null, options); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { /* Ignore */ } } } } if (bitmap == null) { throw new RuntimeException("Skia image region decoder returned null bitmap - image format may not be supported"); } return bitmap; }