react-native-navigation的迁移库

JsDevImageLoader.java 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package com.reactnativenavigation.react;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.graphics.drawable.BitmapDrawable;
  5. import android.graphics.drawable.Drawable;
  6. import android.os.StrictMode;
  7. import android.support.annotation.NonNull;
  8. import com.reactnativenavigation.NavigationApplication;
  9. import java.io.IOException;
  10. import java.net.URL;
  11. public class JsDevImageLoader {
  12. public static Drawable loadIcon(String iconDevUri) {
  13. try {
  14. StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy();
  15. StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
  16. Drawable drawable = tryLoadIcon(iconDevUri);
  17. StrictMode.setThreadPolicy(threadPolicy);
  18. return drawable;
  19. } catch (Exception e) {
  20. throw new RuntimeException(iconDevUri, e);
  21. }
  22. }
  23. @NonNull
  24. private static Drawable tryLoadIcon(String iconDevUri) throws IOException {
  25. URL url = new URL(iconDevUri);
  26. Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
  27. return new BitmapDrawable(NavigationApplication.instance.getResources(), bitmap);
  28. }
  29. }