FileManager.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package app.ara.utils.files;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.res.AssetFileDescriptor;
  5. import android.content.res.AssetManager;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.net.Uri;
  9. import android.util.Log;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. /**
  13. * Created by ptitcois on 27/03/17.
  14. */
  15. public class FileManager {
  16. protected static AssetManager m_asset=null;
  17. protected static Activity m_act = null;
  18. public static void init(Activity a)
  19. {
  20. m_act=a;
  21. m_asset=a.getAssets();
  22. }
  23. private static boolean isInit() throws Exception {
  24. return true;
  25. }
  26. public static InputStream openFile(Context a, String path) throws Exception {
  27. isInit();
  28. InputStream is = null;
  29. try {
  30. is = a.getAssets().open(path);
  31. } catch (IOException e) {
  32. }
  33. return is;
  34. }
  35. private static final String[] IMG_EXT={"", ".jpg" , ".JPG",
  36. ".jpeg" , ".JPEG",
  37. ".bmp", ".BMP",
  38. ".gif", ".GIF",
  39. ".png", ".PNG"
  40. };
  41. private static final String[] MEDIA_EXT={"", ".3gp" , ".3GP",
  42. ".mp4", ".MP4",
  43. ".avi", ".AVI",
  44. ".webm", ".WEBM",
  45. ".mkv", ".MKV",
  46. ".aac", ".AAC",
  47. ".flac", ".FLAC",
  48. ".mid", ".MID",
  49. ".midi", ".MIDI",
  50. ".mp3", ".MP3",
  51. ".3gpp", ".3GPP",
  52. ".wav", ".WAV",
  53. ".ogg", ".OGG"};
  54. private static InputStream testIimage(Context a,String path) throws Exception {
  55. for(int i=0; i<IMG_EXT.length; i++)
  56. {
  57. InputStream is = openFile(a, path+IMG_EXT[i]);
  58. if(is!=null) {
  59. Log.i("app.ara: Open", "Recherche d'image: '"+path+"' OK");
  60. return is;
  61. }
  62. }
  63. Log.i("app.ara: Open", "Recherche d'image: '"+path+"' FAIL");
  64. return null;
  65. }
  66. public static Bitmap openImage(Context a,String path) throws Exception {
  67. isInit();
  68. InputStream is = null;
  69. Bitmap bitmap = null;
  70. try {
  71. is = testIimage(a, path);
  72. bitmap = BitmapFactory.decodeStream(is);
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. return bitmap;
  77. }
  78. public static Uri getUri(Context a, String path) throws Exception {
  79. isInit();
  80. return Uri.parse("file:///android_asset/" + path);
  81. }
  82. private static AssetFileDescriptor testVideo(Context a,String path) {
  83. for(int i=0; i<MEDIA_EXT.length; i++)
  84. {
  85. AssetFileDescriptor is = null;
  86. try {
  87. is = a.getAssets().openFd(path+MEDIA_EXT[i]);
  88. Log.i("app.ara: Open", "Recherche de vidéo: '"+path+"' OK");
  89. if(is!=null)
  90. return is;
  91. } catch (IOException e) {
  92. //e.printStackTrace();
  93. }
  94. }
  95. Log.e("app.ara: Open", "Recherche de vidéo: '"+path+"' FAIL");
  96. return null;
  97. }
  98. public static AssetFileDescriptor getAfdMedia(Context a, String path)
  99. {
  100. return testVideo(a, path);
  101. }
  102. }