Advertisement
RomanByakov

Untitled

Jun 19th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. package ru.happy_giraffe.mypediatrician.utils;
  2.  
  3. import android.content.Context;
  4. import android.content.res.AssetFileDescriptor;
  5. import android.database.Cursor;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Matrix;
  9. import android.media.ExifInterface;
  10. import android.net.Uri;
  11. import android.provider.MediaStore;
  12. import android.provider.OpenableColumns;
  13.  
  14. import com.loopj.android.http.AsyncHttpClient;
  15. import com.loopj.android.http.RequestParams;
  16.  
  17. import org.apache.commons.io.output.ByteArrayOutputStream;
  18. import org.json.JSONArray;
  19. import org.json.JSONObject;
  20.  
  21. import java.io.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.FileNotFoundException;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26.  
  27. import cz.msebera.android.httpclient.Header;
  28. import ru.happy_giraffe.giraffesdk.Giraffe;
  29. import ru.happy_giraffe.mypediatrician.BuildConfig;
  30. import ru.happy_giraffe.mypediatrician.R;
  31.  
  32. /**
  33. * Created by RomanByakov on 15.09.16.
  34. */
  35.  
  36. public class ImageUploader {
  37.  
  38. private ImageUploader() {
  39. }
  40.  
  41. private static volatile ImageUploader ourInstance = new ImageUploader();
  42.  
  43. public static ImageUploader getInstance() {
  44. if (ourInstance == null) {
  45. ourInstance = new ImageUploader();
  46. }
  47. return ourInstance;
  48. }
  49.  
  50. public void uploadPhoto(final String fileName, final InputStream myInputStream, Context context, final FileUploadRequestListener listener) {
  51. AsyncHttpClient client = new AsyncHttpClient();
  52. client.setTimeout(20000);
  53.  
  54. RequestParams requestParams = new RequestParams();
  55. client.addHeader("access-token", Giraffe.getInstance(context).getCurrentAccessToken());
  56. client.addHeader("app-id", BuildConfig.appId);
  57. requestParams.put("photo", myInputStream, fileName, "image/png");
  58.  
  59. String url = BuildConfig.host + "/api/photo/";
  60. String method = "POST";
  61.  
  62. client.post(url, requestParams, new JsonHttpResponseHandlerWithLogs(url, method, requestParams) {
  63.  
  64. @Override
  65. public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
  66. super.onSuccess(statusCode, headers, response);
  67. listener.onSuccess(response.optString("url"), response.optInt("new_photo_id"));
  68. }
  69.  
  70. @Override
  71. public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
  72. super.onFailure(statusCode, headers, responseString, throwable);
  73. listener.onError("Не удалось загрузить фото");
  74. }
  75.  
  76. @Override
  77. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
  78. super.onFailure(statusCode, headers, throwable, errorResponse);
  79. listener.onError("Не удалось загрузить фото");
  80. }
  81.  
  82. @Override
  83. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
  84. super.onFailure(statusCode, headers, throwable, errorResponse);
  85. listener.onError("Не удалось загрузить фото");
  86. }
  87. });
  88. }
  89.  
  90. public String getHtmlPreview(String filePath) {
  91. return "<img src=\"" + filePath + "\" width=\"100%\" style=\"opacity:0.4;filter:alpha(opacity=40)\">";
  92. }
  93.  
  94. public String getCompletedHtml(String filePath) {
  95. return "<img src=\"" + filePath + "\" width=\"100%\"><p></p><br>";
  96. }
  97.  
  98. public void prepareAndUpload(String filePath, Context context, boolean isCamera, FileUploadRequestListener listener) {
  99.  
  100. Uri uri = null;
  101. if (isCamera) {
  102. uri = Uri.fromFile(new File(filePath));
  103. } else {
  104. uri = Uri.parse(filePath);
  105. }
  106.  
  107. String name = "photo";
  108. int orientation = 0;
  109.  
  110. // get orientation
  111. if (isCamera) {
  112. ExifInterface ei = null;
  113. try {
  114. ei = new ExifInterface(filePath);
  115. } catch (IOException ex) {
  116. ex.printStackTrace();
  117. }
  118. orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
  119. name = filePath.substring(filePath.lastIndexOf("/") + 1);
  120. } else {
  121. Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null);
  122.  
  123. String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
  124. int nameIndex = 0;
  125. try {
  126.  
  127. if (returnCursor != null && returnCursor.moveToFirst()) {
  128. int index = returnCursor.getColumnIndex(orientationColumn[0]);
  129. orientation = returnCursor.getInt(index);
  130. nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
  131. // get filename
  132. name = returnCursor.getString(nameIndex);
  133.  
  134. System.out.println("get orientation fin");
  135. System.out.println("get filename fin");
  136. returnCursor.close();
  137. System.out.println("cursor close");
  138. }
  139. } catch (Exception er) {
  140.  
  141. }
  142. }
  143.  
  144. // get bitmap
  145. Bitmap bitmap = readBitmap(uri, context);
  146. System.out.println("get bitmap fin");
  147. // autorotate
  148. if (orientation != 0) {
  149. bitmap = rotateImage(bitmap, new Float(orientation));
  150. System.out.println("autorotate fin");
  151. }
  152.  
  153. InputStream inputStream = null;
  154.  
  155. // get stream
  156. if (orientation != 0) {
  157.  
  158. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  159. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
  160. inputStream = new ByteArrayInputStream(stream.toByteArray());
  161. if (stream != null) {
  162. try {
  163. stream.close();
  164. } catch (IOException e) {
  165. e.printStackTrace();
  166. }
  167. }
  168.  
  169. } else {
  170. System.out.println("without rotate");
  171. try {
  172. inputStream = context.getContentResolver().openInputStream(uri);
  173. } catch (FileNotFoundException e) {
  174. e.printStackTrace();
  175. }
  176. }
  177.  
  178. // load photo
  179. uploadPhoto(name, inputStream, context, listener);
  180. }
  181.  
  182. private Bitmap rotateImage(Bitmap source, float angle) {
  183. Bitmap retVal;
  184.  
  185. Matrix matrix = new Matrix();
  186. matrix.postRotate(angle);
  187. retVal = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
  188. return retVal;
  189. }
  190.  
  191. private Bitmap readBitmap(Uri selectedImage, Context context) {
  192. Bitmap bm = null;
  193. BitmapFactory.Options options = new BitmapFactory.Options();
  194. options.inSampleSize = 5;
  195. AssetFileDescriptor fileDescriptor = null;
  196. try {
  197. fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
  198. } catch (FileNotFoundException e) {
  199. e.printStackTrace();
  200. } finally {
  201. try {
  202. bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
  203. fileDescriptor.close();
  204. } catch (IOException e) {
  205. e.printStackTrace();
  206. }
  207. }
  208. return bm;
  209. }
  210.  
  211.  
  212. public interface FileUploadRequestListener {
  213.  
  214. void onSuccess(String url, int photoId);
  215.  
  216. void onError(String error);
  217. }
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement