react-native-navigation的迁移库

IconUtils.java 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package com.reactnativenavigation.utils;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.drawable.BitmapDrawable;
  6. import android.graphics.drawable.Drawable;
  7. import android.net.Uri;
  8. import com.reactnativenavigation.BuildConfig;
  9. import java.net.URL;
  10. /**
  11. * Created by yedidyak on 29/05/2016.
  12. */
  13. public class IconUtils {
  14. public static final String LOCAL_RESOURCE_URI_SCHEME = "res";
  15. private static ResourceDrawableIdHelper sResDrawableIdHelper = new ResourceDrawableIdHelper();
  16. public static Drawable getIcon(Context ctx, String iconSource) {
  17. if (iconSource == null) {
  18. return null;
  19. }
  20. try {
  21. Drawable icon;
  22. Uri iconUri = getIconUri(ctx, iconSource);
  23. if (LOCAL_RESOURCE_URI_SCHEME.equals(iconUri.getScheme())) {
  24. icon = sResDrawableIdHelper.getResourceDrawable(ctx, iconSource);
  25. } else {
  26. URL url = new URL(iconUri.toString());
  27. Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
  28. icon = new BitmapDrawable(bitmap);
  29. }
  30. return icon;
  31. } catch (Exception e) {
  32. if (BuildConfig.DEBUG) {
  33. e.printStackTrace();
  34. }
  35. }
  36. return null;
  37. }
  38. private static Uri getIconUri(Context context, String iconSource) {
  39. Uri ret = null;
  40. if (iconSource != null) {
  41. try {
  42. ret = Uri.parse(iconSource);
  43. // Verify scheme is set, so that relative uri (used by static resources) are not handled.
  44. if (ret.getScheme() == null) {
  45. ret = null;
  46. }
  47. } catch (Exception e) {
  48. // Ignore malformed uri, then attempt to extract resource ID.
  49. }
  50. if (ret == null) {
  51. ret = sResDrawableIdHelper.getResourceDrawableUri(context, iconSource);
  52. }
  53. }
  54. return ret;
  55. }
  56. }