Guest User

Untitled

a guest
Nov 16th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.02 KB | None | 0 0
  1. package com.pratikbutani.pdf;
  2.  
  3. import android.content.ActivityNotFoundException;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.content.pm.ResolveInfo;
  8. import android.graphics.Bitmap;
  9. import android.graphics.BitmapFactory;
  10. import android.net.Uri;
  11. import android.support.v4.content.FileProvider;
  12.  
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.util.Arrays;
  21. import java.util.List;
  22.  
  23. import static com.pratikbutani.pdf.LogUtils.LOGD;
  24. import static com.pratikbutani.pdf.LogUtils.LOGE;
  25.  
  26. /**
  27. * Created by k2 on 30/6/17.
  28. */
  29.  
  30. public class FileUtils {
  31.  
  32. private static final String extensions[] = new String[]{"avi", "3gp", "mp4", "mp3", "jpeg", "jpg",
  33. "gif", "png",
  34. "pdf", "docx", "doc", "xls", "xlsx", "csv", "ppt", "pptx",
  35. "txt", "zip", "rar"};
  36.  
  37.  
  38. public static void openFile(Context context, File url) throws ActivityNotFoundException,
  39. IOException {
  40. // Create URI
  41. //Uri uri = Uri.fromFile(url);
  42.  
  43. //TODO you want to use this method then create file provider in androidmanifest.xml with fileprovider name
  44.  
  45. Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", url);
  46.  
  47. String urlString = url.toString().toLowerCase();
  48.  
  49. Intent intent = new Intent(Intent.ACTION_VIEW);
  50.  
  51. /**
  52. * Security
  53. */
  54. List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
  55. for (ResolveInfo resolveInfo : resInfoList) {
  56. String packageName = resolveInfo.activityInfo.packageName;
  57. context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
  58. }
  59.  
  60. // Check what kind of file you are trying to open, by comparing the url with extensions.
  61. // When the if condition is matched, plugin sets the correct intent (mime) type,
  62. // so Android knew what application to use to open the file
  63. if (urlString.toLowerCase().toLowerCase().contains(".doc")
  64. || urlString.toLowerCase().contains(".docx")) {
  65. // Word document
  66. intent.setDataAndType(uri, "application/msword");
  67. } else if (urlString.toLowerCase().contains(".pdf")) {
  68. // PDF file
  69. intent.setDataAndType(uri, "application/pdf");
  70. } else if (urlString.toLowerCase().contains(".ppt")
  71. || urlString.toLowerCase().contains(".pptx")) {
  72. // Powerpoint file
  73. intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
  74. } else if (urlString.toLowerCase().contains(".xls")
  75. || urlString.toLowerCase().contains(".xlsx")) {
  76. // Excel file
  77. intent.setDataAndType(uri, "application/vnd.ms-excel");
  78. } else if (urlString.toLowerCase().contains(".zip")
  79. || urlString.toLowerCase().contains(".rar")) {
  80. // ZIP file
  81. intent.setDataAndType(uri, "application/trap");
  82. } else if (urlString.toLowerCase().contains(".rtf")) {
  83. // RTF file
  84. intent.setDataAndType(uri, "application/rtf");
  85. } else if (urlString.toLowerCase().contains(".wav")
  86. || urlString.toLowerCase().contains(".mp3")) {
  87. // WAV/MP3 audio file
  88. intent.setDataAndType(uri, "audio/*");
  89. } else if (urlString.toLowerCase().contains(".gif")) {
  90. // GIF file
  91. intent.setDataAndType(uri, "image/gif");
  92. } else if (urlString.toLowerCase().contains(".jpg")
  93. || urlString.toLowerCase().contains(".jpeg")
  94. || urlString.toLowerCase().contains(".png")) {
  95. // JPG file
  96. intent.setDataAndType(uri, "image/jpeg");
  97. } else if (urlString.toLowerCase().contains(".txt")) {
  98. // Text file
  99. intent.setDataAndType(uri, "text/plain");
  100. } else if (urlString.toLowerCase().contains(".3gp")
  101. || urlString.toLowerCase().contains(".mpg")
  102. || urlString.toLowerCase().contains(".mpeg")
  103. || urlString.toLowerCase().contains(".mpe")
  104. || urlString.toLowerCase().contains(".mp4")
  105. || urlString.toLowerCase().contains(".avi")) {
  106. // Video files
  107. intent.setDataAndType(uri, "video/*");
  108. } else {
  109. // if you want you can also define the intent type for any other file
  110.  
  111. // additionally use else clause below, to manage other unknown extensions
  112. // in this case, Android will show all applications installed on the device
  113. // so you can choose which application to use
  114. intent.setDataAndType(uri, "*/*");
  115. }
  116.  
  117. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  118. context.startActivity(intent);
  119. }
  120.  
  121. /**
  122. * Get Path of App which contains Files
  123. *
  124. * @return path of root dir
  125. */
  126. public static String getAppPath(Context context) {
  127. File dir = new File(android.os.Environment.getExternalStorageDirectory()
  128. + File.separator
  129. + context.getResources().getString(R.string.app_name)
  130. + File.separator);
  131. if (!dir.exists()) {
  132. dir.mkdir();
  133. }
  134. return dir.getPath() + File.separator;
  135. }
  136.  
  137. /***
  138. * Copy File
  139. *
  140. * @param src
  141. * @param dst
  142. * @throws IOException
  143. */
  144. public static void copy(File src, File dst) {
  145. InputStream in;
  146. OutputStream out;
  147. try {
  148. in = new FileInputStream(src);
  149. out = new FileOutputStream(dst);
  150.  
  151. String tempExt = FileUtils.getExtension(dst.getPath());
  152.  
  153. if (tempExt.equals("jpeg") || tempExt.equals("jpg") || tempExt.equals("gif")
  154. || tempExt.equals("png")) {
  155. if (out != null) {
  156.  
  157. Bitmap bit = BitmapFactory.decodeFile(src.getPath());
  158. LOGD("Bitmap : " + bit);
  159.  
  160. if (bit.getWidth() > 700) {
  161. if (bit.getHeight() > 700)
  162. bit = Bitmap.createScaledBitmap(bit, 700, 700, true);
  163. else
  164. bit = Bitmap.createScaledBitmap(bit, 700, bit.getHeight(), true);
  165. } else {
  166. if (bit.getHeight() > 700)
  167. bit = Bitmap.createScaledBitmap(bit, bit.getWidth(), 700, true);
  168. else
  169. bit = Bitmap.createScaledBitmap(bit, bit.getWidth(), bit.getHeight(), true);
  170. }
  171.  
  172. bit.compress(Bitmap.CompressFormat.JPEG, 90, out);
  173. }
  174. LOGD("File Compressed...");
  175. } else {
  176.  
  177. // Transfer bytes from in to out
  178. byte[] buf = new byte[1024 * 4];
  179. int len;
  180. while ((len = in.read(buf)) > 0) {
  181. out.write(buf, 0, len);
  182. }
  183. }
  184.  
  185. in.close();
  186. out.close();
  187.  
  188. } catch (FileNotFoundException e) {
  189. // TODO Auto-generated catch block
  190. LOGE("Compressing ERror : " + e.getLocalizedMessage());
  191. } catch (IOException e) {
  192. // TODO Auto-generated catch block
  193. LOGE("Compressing ERror IOE : " + e.getLocalizedMessage());
  194. } catch (Exception e) {
  195. // TODO: handle exception
  196. LOGE("Compressing ERror Other: " + e.getLocalizedMessage());
  197. }
  198. }
  199.  
  200. /***
  201. * Move File
  202. *
  203. * @param src
  204. * @param dst
  205. * @throws IOException
  206. */
  207. public static void move(File src, File dst) {
  208. InputStream in;
  209. OutputStream out;
  210. try {
  211. in = new FileInputStream(src);
  212. out = new FileOutputStream(dst);
  213.  
  214. String tempExt = FileUtils.getExtension(dst.getPath());
  215.  
  216. if (tempExt.equals("jpeg") || tempExt.equals("jpg") || tempExt.equals("gif")
  217. || tempExt.equals("png")) {
  218. if (out != null) {
  219.  
  220. Bitmap bit = BitmapFactory.decodeFile(src.getPath());
  221. LOGD("Bitmap : " + bit);
  222.  
  223. if (bit.getWidth() > 700 || bit.getHeight() > 700) {
  224. bit = Bitmap.createScaledBitmap(bit, 700, 700, true);
  225. }
  226. bit.compress(Bitmap.CompressFormat.JPEG, 90, out);
  227. }
  228. LOGD("File Compressed...");
  229. } else {
  230.  
  231. // Transfer bytes from in to out
  232. byte[] buf = new byte[1024 * 4];
  233. int len;
  234. while ((len = in.read(buf)) > 0) {
  235. out.write(buf, 0, len);
  236. }
  237. }
  238.  
  239. in.close();
  240. out.close();
  241.  
  242. /**
  243. * Delete File from Source folder...
  244. */
  245. if (src.delete())
  246. LOGD("File Successfully Copied...");
  247.  
  248. } catch (FileNotFoundException e) {
  249. // TODO Auto-generated catch block
  250. LOGE("Compressing ERror : " + e.getLocalizedMessage());
  251. } catch (IOException e) {
  252. // TODO Auto-generated catch block
  253. LOGE("Compressing ERror IOE : " + e.getLocalizedMessage());
  254. } catch (Exception e) {
  255. // TODO: handle exception
  256. LOGE("Compressing ERror Other: " + e.getLocalizedMessage());
  257. }
  258. }
  259.  
  260. /**
  261. * Is Valid Extension
  262. *
  263. * @param ext
  264. * @return
  265. */
  266. public static boolean isValidExtension(String ext) {
  267. return Arrays.asList(extensions).contains(ext);
  268.  
  269. }
  270.  
  271. /**
  272. * Return Extension of given path without dot(.)
  273. *
  274. * @param path
  275. * @return
  276. */
  277. public static String getExtension(String path) {
  278. return path.contains(".") ? path.substring(path.lastIndexOf(".") + 1).toLowerCase() : "";
  279. }
  280. }
Add Comment
Please, Sign In to add comment