@phucprime/react-native-image-editor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +247 -0
  3. package/android/build.gradle +62 -0
  4. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  5. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  6. package/android/gradlew +240 -0
  7. package/android/gradlew.bat +91 -0
  8. package/android/libs/photo-editor-android.jar +0 -0
  9. package/android/src/main/AndroidManifest.xml +5 -0
  10. package/android/src/main/java/com/ahmedadeltito/photoeditor/BaseActivity.java +300 -0
  11. package/android/src/main/java/com/ahmedadeltito/photoeditor/ColorPickerAdapter.java +129 -0
  12. package/android/src/main/java/com/ahmedadeltito/photoeditor/EmojiAdapter.java +91 -0
  13. package/android/src/main/java/com/ahmedadeltito/photoeditor/EmojiFragment.java +56 -0
  14. package/android/src/main/java/com/ahmedadeltito/photoeditor/GalleryUtils.java +73 -0
  15. package/android/src/main/java/com/ahmedadeltito/photoeditor/ImageAdapter.java +69 -0
  16. package/android/src/main/java/com/ahmedadeltito/photoeditor/ImageFragment.java +93 -0
  17. package/android/src/main/java/com/ahmedadeltito/photoeditor/MainActivity.java +29 -0
  18. package/android/src/main/java/com/ahmedadeltito/photoeditor/MediaActivity.java +95 -0
  19. package/android/src/main/java/com/ahmedadeltito/photoeditor/PhotoEditorActivity.java +1055 -0
  20. package/android/src/main/java/com/ahmedadeltito/photoeditor/TranslationService.java +31 -0
  21. package/android/src/main/java/com/ahmedadeltito/photoeditor/UtilFunctions.java +176 -0
  22. package/android/src/main/java/com/ahmedadeltito/photoeditor/widget/RealtimeBlurView.java +359 -0
  23. package/android/src/main/java/com/ahmedadeltito/photoeditor/widget/SlidingUpPanelLayout.java +1527 -0
  24. package/android/src/main/java/com/ahmedadeltito/photoeditor/widget/ViewDragHelper.java +1466 -0
  25. package/android/src/main/java/ui/photoeditor/RNPhotoEditorModule.java +108 -0
  26. package/android/src/main/java/ui/photoeditor/RNPhotoEditorPackage.java +29 -0
  27. package/android/src/main/res/drawable/above_shadow.xml +7 -0
  28. package/android/src/main/res/drawable/below_shadow.xml +7 -0
  29. package/android/src/main/res/drawable/fading_shadow.xml +7 -0
  30. package/android/src/main/res/drawable/rounded_border_text_view.xml +13 -0
  31. package/android/src/main/res/drawable-xhdpi/aa.png +0 -0
  32. package/android/src/main/res/drawable-xhdpi/bb.png +0 -0
  33. package/android/src/main/res/drawable-xhdpi/c.png +0 -0
  34. package/android/src/main/res/drawable-xhdpi/cc.png +0 -0
  35. package/android/src/main/res/drawable-xhdpi/d.png +0 -0
  36. package/android/src/main/res/drawable-xhdpi/dd.png +0 -0
  37. package/android/src/main/res/drawable-xhdpi/e.png +0 -0
  38. package/android/src/main/res/drawable-xhdpi/ee.png +0 -0
  39. package/android/src/main/res/drawable-xhdpi/i.png +0 -0
  40. package/android/src/main/res/drawable-xhdpi/j.png +0 -0
  41. package/android/src/main/res/drawable-xhdpi/k.png +0 -0
  42. package/android/src/main/res/drawable-xhdpi/l.png +0 -0
  43. package/android/src/main/res/drawable-xhdpi/m.png +0 -0
  44. package/android/src/main/res/drawable-xhdpi/n.png +0 -0
  45. package/android/src/main/res/drawable-xhdpi/o.png +0 -0
  46. package/android/src/main/res/drawable-xhdpi/p.png +0 -0
  47. package/android/src/main/res/drawable-xhdpi/q.png +0 -0
  48. package/android/src/main/res/drawable-xhdpi/r.png +0 -0
  49. package/android/src/main/res/drawable-xhdpi/s.png +0 -0
  50. package/android/src/main/res/drawable-xhdpi/t.png +0 -0
  51. package/android/src/main/res/drawable-xhdpi/u.png +0 -0
  52. package/android/src/main/res/drawable-xhdpi/v.png +0 -0
  53. package/android/src/main/res/drawable-xhdpi/w.png +0 -0
  54. package/android/src/main/res/drawable-xhdpi/x.png +0 -0
  55. package/android/src/main/res/drawable-xhdpi/y.png +0 -0
  56. package/android/src/main/res/drawable-xhdpi/z.png +0 -0
  57. package/android/src/main/res/layout/activity_main.xml +28 -0
  58. package/android/src/main/res/layout/activity_photo_editor.xml +316 -0
  59. package/android/src/main/res/layout/add_text_popup_window.xml +47 -0
  60. package/android/src/main/res/layout/color_picker_item_list.xml +15 -0
  61. package/android/src/main/res/layout/fragment_main_photo_edit_emoji.xml +11 -0
  62. package/android/src/main/res/layout/fragment_main_photo_edit_image.xml +10 -0
  63. package/android/src/main/res/layout/fragment_photo_edit_emoji_item_list.xml +17 -0
  64. package/android/src/main/res/layout/fragment_photo_edit_image_item_list.xml +16 -0
  65. package/android/src/main/res/raw/emojioneandroid +0 -0
  66. package/android/src/main/res/raw/eventtusicons +0 -0
  67. package/android/src/main/res/raw/font_awesome_solid.otf +0 -0
  68. package/android/src/main/res/raw/icomoon.ttf +0 -0
  69. package/android/src/main/res/values/arrays.xml +447 -0
  70. package/android/src/main/res/values/attrs.xml +30 -0
  71. package/android/src/main/res/values/colors.xml +21 -0
  72. package/android/src/main/res/values/dimens.xml +4 -0
  73. package/android/src/main/res/values/strings.xml +33 -0
  74. package/android/src/main/res/values/styles.xml +27 -0
  75. package/ios/RNImageEditor.podspec +26 -0
  76. package/ios/RNPhotoEditor.h +13 -0
  77. package/ios/RNPhotoEditor.m +163 -0
  78. package/ios/RNPhotoEditor.xcodeproj/project.pbxproj +362 -0
  79. package/lib/index.d.ts +125 -0
  80. package/lib/index.d.ts.map +1 -0
  81. package/lib/index.js +114 -0
  82. package/lib/index.js.map +1 -0
  83. package/package.json +73 -0
  84. package/react-native-image-editor.podspec +39 -0
  85. package/src/index.ts +232 -0
@@ -0,0 +1,1055 @@
1
+ package com.ahmedadeltito.photoeditor;
2
+
3
+ import android.annotation.TargetApi;
4
+ import android.app.Activity;
5
+ import android.app.AlertDialog;
6
+ import android.content.ContentUris;
7
+ import android.content.Context;
8
+ import android.content.DialogInterface;
9
+ import android.content.Intent;
10
+ import android.content.pm.PackageManager;
11
+ import android.content.res.Resources;
12
+ import android.graphics.Bitmap;
13
+ import android.graphics.BitmapFactory;
14
+ import android.graphics.Matrix;
15
+ import android.graphics.Typeface;
16
+ import android.graphics.drawable.BitmapDrawable;
17
+ import android.net.Uri;
18
+ import android.os.Build;
19
+ import android.os.Bundle;
20
+ import android.os.CountDownTimer;
21
+ import android.os.Environment;
22
+ import android.provider.DocumentsContract;
23
+ import android.provider.MediaStore;
24
+ import android.util.Log;
25
+ import android.view.Gravity;
26
+ import android.view.LayoutInflater;
27
+ import android.view.View;
28
+ import android.view.WindowManager;
29
+ import android.view.inputmethod.InputMethodManager;
30
+ import android.widget.EditText;
31
+ import android.widget.FrameLayout;
32
+ import android.widget.ImageView;
33
+ import android.widget.LinearLayout;
34
+ import android.widget.PopupWindow;
35
+ import android.widget.RelativeLayout;
36
+ import android.widget.TextView;
37
+ import android.widget.Toast;
38
+ import androidx.annotation.NonNull;
39
+ import androidx.appcompat.app.AppCompatActivity;
40
+ import androidx.core.app.ActivityCompat;
41
+ import androidx.core.content.PermissionChecker;
42
+ import androidx.exifinterface.media.ExifInterface;
43
+ import androidx.fragment.app.Fragment;
44
+ import androidx.fragment.app.FragmentManager;
45
+ import androidx.fragment.app.FragmentStatePagerAdapter;
46
+ import androidx.recyclerview.widget.LinearLayoutManager;
47
+ import androidx.recyclerview.widget.RecyclerView;
48
+ import androidx.viewpager.widget.ViewPager;
49
+ import com.ahmedadeltito.photoeditor.widget.SlidingUpPanelLayout;
50
+ import com.ahmedadeltito.photoeditorsdk.BrushDrawingView;
51
+ import com.ahmedadeltito.photoeditorsdk.OnPhotoEditorSDKListener;
52
+ import com.ahmedadeltito.photoeditorsdk.PhotoEditorSDK;
53
+ import com.ahmedadeltito.photoeditorsdk.ViewType;
54
+ import com.viewpagerindicator.PageIndicator;
55
+ import com.yalantis.ucrop.UCrop;
56
+ import com.yalantis.ucrop.UCropActivity;
57
+ import java.io.BufferedOutputStream;
58
+ import java.io.File;
59
+ import java.io.FileOutputStream;
60
+ import java.io.IOException;
61
+ import java.io.InputStream;
62
+ import java.text.SimpleDateFormat;
63
+ import java.util.ArrayList;
64
+ import java.util.Date;
65
+ import java.util.List;
66
+ import java.util.UUID;
67
+ import ui.photoeditor.R;
68
+
69
+ public class PhotoEditorActivity
70
+ extends AppCompatActivity
71
+ implements View.OnClickListener, OnPhotoEditorSDKListener {
72
+
73
+ public static Typeface emojiFont = null;
74
+
75
+ protected static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE_GALLERY = 0x1;
76
+ final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
77
+
78
+ private final String TAG = "PhotoEditorActivity";
79
+ private RelativeLayout parentImageRelativeLayout;
80
+ private RecyclerView drawingViewColorPickerRecyclerView;
81
+ private TextView undoTextView, undoTextTextView, doneDrawingTextView, eraseDrawingTextView;
82
+ private SlidingUpPanelLayout mLayout;
83
+ private View topShadow;
84
+ private RelativeLayout topShadowRelativeLayout;
85
+ private View bottomShadow;
86
+ private RelativeLayout bottomShadowRelativeLayout;
87
+ private ArrayList<Integer> colorPickerColors;
88
+ private int colorCodeTextView = -1;
89
+ private PhotoEditorSDK photoEditorSDK;
90
+ private String selectedImagePath;
91
+ private int imageOrientation;
92
+ private FrameLayout overlay;
93
+
94
+ // CROP OPTION
95
+ private boolean cropperCircleOverlay = false;
96
+ private boolean freeStyleCropEnabled = false;
97
+ private boolean showCropGuidelines = true;
98
+ private boolean hideBottomControls = false;
99
+
100
+ private ImageView photoEditImageView;
101
+
102
+ @Override
103
+ protected void onCreate(Bundle savedInstanceState) {
104
+ super.onCreate(savedInstanceState);
105
+ setContentView(R.layout.activity_photo_editor);
106
+
107
+ getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
108
+
109
+ selectedImagePath = getIntent().getExtras().getString("selectedImagePath");
110
+ if (selectedImagePath.contains("content://")) {
111
+ selectedImagePath = getPath(Uri.parse(selectedImagePath));
112
+ }
113
+ Log.d("PhotoEditorSDK", "Selected image path: " + selectedImagePath);
114
+
115
+ BitmapFactory.Options options = new BitmapFactory.Options();
116
+ options.inSampleSize = 1;
117
+ Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath, options);
118
+
119
+ Bitmap rotatedBitmap;
120
+ try {
121
+ ExifInterface exif = new ExifInterface(selectedImagePath);
122
+ imageOrientation =
123
+ exif.getAttributeInt(
124
+ ExifInterface.TAG_ORIENTATION,
125
+ ExifInterface.ORIENTATION_UNDEFINED
126
+ );
127
+ rotatedBitmap = rotateBitmap(bitmap, imageOrientation, false);
128
+ } catch (IOException e) {
129
+ rotatedBitmap = bitmap;
130
+ imageOrientation = ExifInterface.ORIENTATION_NORMAL;
131
+
132
+ e.printStackTrace();
133
+ }
134
+
135
+ Typeface newFont = getFontFromRes(R.raw.eventtusicons);
136
+ Typeface fontAwesome = getFontFromRes(R.raw.font_awesome_solid);
137
+ //icomoon
138
+ Typeface icomoon = getFontFromRes(R.raw.icomoon);
139
+
140
+ emojiFont = getFontFromRes(R.raw.emojioneandroid);
141
+
142
+ BrushDrawingView brushDrawingView = (BrushDrawingView) findViewById(R.id.drawing_view);
143
+ drawingViewColorPickerRecyclerView =
144
+ (RecyclerView) findViewById(R.id.drawing_view_color_picker_recycler_view);
145
+ parentImageRelativeLayout = (RelativeLayout) findViewById(R.id.parent_image_rl);
146
+ TextView closeTextView = (TextView) findViewById(R.id.close_tv);
147
+ TextView addTextView = (TextView) findViewById(R.id.add_text_tv);
148
+ TextView addPencil = (TextView) findViewById(R.id.add_pencil_tv);
149
+ RelativeLayout deleteRelativeLayout = (RelativeLayout) findViewById(R.id.delete_rl);
150
+ TextView deleteTextView = (TextView) findViewById(R.id.delete_tv);
151
+ TextView addImageEmojiTextView = (TextView) findViewById(R.id.add_image_emoji_tv);
152
+ TextView addCropTextView = (TextView) findViewById(R.id.add_crop_tv);
153
+ TextView saveTextView = (TextView) findViewById(R.id.save_tv);
154
+ TextView addRotateTextView = (TextView) findViewById(R.id.add_rotate_tv);
155
+
156
+ undoTextView = (TextView) findViewById(R.id.undo_tv);
157
+ undoTextTextView = (TextView) findViewById(R.id.undo_text_tv);
158
+ doneDrawingTextView = (TextView) findViewById(R.id.done_drawing_tv);
159
+ eraseDrawingTextView = (TextView) findViewById(R.id.erase_drawing_tv);
160
+ TextView clearAllTextView = (TextView) findViewById(R.id.clear_all_tv);
161
+ TextView goToNextTextView = (TextView) findViewById(R.id.go_to_next_screen_tv);
162
+ photoEditImageView = (ImageView) findViewById(R.id.photo_edit_iv);
163
+ mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
164
+ topShadow = findViewById(R.id.top_shadow);
165
+ topShadowRelativeLayout = (RelativeLayout) findViewById(R.id.top_parent_rl);
166
+ bottomShadow = findViewById(R.id.bottom_shadow);
167
+ bottomShadowRelativeLayout = (RelativeLayout) findViewById(R.id.bottom_parent_rl);
168
+ // Get a reference to the overlay
169
+ overlay = findViewById(R.id.overlay);
170
+
171
+ ViewPager pager = (ViewPager) findViewById(R.id.image_emoji_view_pager);
172
+ PageIndicator indicator = (PageIndicator) findViewById(R.id.image_emoji_indicator);
173
+
174
+ photoEditImageView.setImageBitmap(rotatedBitmap);
175
+
176
+ closeTextView.setTypeface(icomoon);
177
+ addTextView.setTypeface(icomoon);
178
+ addPencil.setTypeface(icomoon);
179
+ addImageEmojiTextView.setTypeface(icomoon);
180
+ addCropTextView.setTypeface(icomoon);
181
+ saveTextView.setTypeface(icomoon);
182
+ undoTextView.setTypeface(icomoon);
183
+ clearAllTextView.setTypeface(icomoon);
184
+ goToNextTextView.setTypeface(icomoon);
185
+ deleteTextView.setTypeface(icomoon);
186
+ addRotateTextView.setTypeface(icomoon);
187
+
188
+ // set translation for text
189
+ eraseDrawingTextView.setText(getTranslation("eraserTitle"));
190
+ doneDrawingTextView.setText(getTranslation("doneTitle"));
191
+
192
+ final List<Fragment> fragmentsList = new ArrayList<>();
193
+
194
+ ImageFragment imageFragment = new ImageFragment();
195
+ ArrayList stickers = (ArrayList<Integer>) getIntent()
196
+ .getExtras()
197
+ .getSerializable("stickers");
198
+ if (stickers != null && stickers.size() > 0) {
199
+ Bundle bundle = new Bundle();
200
+ bundle.putSerializable("stickers", stickers);
201
+
202
+ imageFragment.setArguments(bundle);
203
+ }
204
+
205
+ fragmentsList.add(imageFragment);
206
+
207
+ EmojiFragment emojiFragment = new EmojiFragment();
208
+ fragmentsList.add(emojiFragment);
209
+
210
+ PreviewSlidePagerAdapter adapter = new PreviewSlidePagerAdapter(
211
+ getSupportFragmentManager(),
212
+ fragmentsList
213
+ );
214
+ pager.setAdapter(adapter);
215
+ pager.setOffscreenPageLimit(5);
216
+ indicator.setViewPager(pager);
217
+
218
+ photoEditorSDK =
219
+ new PhotoEditorSDK.PhotoEditorSDKBuilder(PhotoEditorActivity.this)
220
+ .parentView(parentImageRelativeLayout) // add parent image view
221
+ .childView(photoEditImageView) // add the desired image view
222
+ .deleteView(deleteRelativeLayout) // add the deleted view that will appear during the movement of the views
223
+ .brushDrawingView(brushDrawingView) // add the brush drawing view that is responsible for drawing on the image view
224
+ .buildPhotoEditorSDK(); // build photo editor sdk
225
+ photoEditorSDK.setOnPhotoEditorSDKListener(this);
226
+
227
+ pager.addOnPageChangeListener(
228
+ new ViewPager.OnPageChangeListener() {
229
+ @Override
230
+ public void onPageScrolled(
231
+ int position,
232
+ float positionOffset,
233
+ int positionOffsetPixels
234
+ ) {}
235
+
236
+ @Override
237
+ public void onPageSelected(int position) {
238
+ if (position == 0) mLayout.setScrollableView(
239
+ ((ImageFragment) fragmentsList.get(position)).imageRecyclerView
240
+ ); else if (position == 1) mLayout.setScrollableView(
241
+ ((EmojiFragment) fragmentsList.get(position)).emojiRecyclerView
242
+ );
243
+ }
244
+
245
+ @Override
246
+ public void onPageScrollStateChanged(int state) {}
247
+ }
248
+ );
249
+
250
+ closeTextView.setOnClickListener(this);
251
+ addImageEmojiTextView.setOnClickListener(this);
252
+ addCropTextView.setOnClickListener(this);
253
+ addTextView.setOnClickListener(this);
254
+ addPencil.setOnClickListener(this);
255
+ saveTextView.setOnClickListener(this);
256
+ undoTextView.setOnClickListener(this);
257
+ undoTextTextView.setOnClickListener(this);
258
+ doneDrawingTextView.setOnClickListener(this);
259
+ eraseDrawingTextView.setOnClickListener(this);
260
+ clearAllTextView.setOnClickListener(this);
261
+ goToNextTextView.setOnClickListener(this);
262
+ addRotateTextView.setOnClickListener(this);
263
+
264
+ ArrayList<Integer> intentColors = (ArrayList<Integer>) getIntent()
265
+ .getExtras()
266
+ .getSerializable("colorPickerColors");
267
+
268
+ colorPickerColors = new ArrayList<>();
269
+ if (intentColors != null) {
270
+ colorPickerColors = intentColors;
271
+ } else {
272
+ colorPickerColors.add(getResources().getColor(R.color.black));
273
+ colorPickerColors.add(getResources().getColor(R.color.blue_color_picker));
274
+ colorPickerColors.add(getResources().getColor(R.color.brown_color_picker));
275
+ colorPickerColors.add(getResources().getColor(R.color.green_color_picker));
276
+ colorPickerColors.add(getResources().getColor(R.color.orange_color_picker));
277
+ colorPickerColors.add(getResources().getColor(R.color.red_color_picker));
278
+ colorPickerColors.add(getResources().getColor(R.color.red_orange_color_picker));
279
+ colorPickerColors.add(getResources().getColor(R.color.sky_blue_color_picker));
280
+ colorPickerColors.add(getResources().getColor(R.color.violet_color_picker));
281
+ colorPickerColors.add(getResources().getColor(R.color.white));
282
+ colorPickerColors.add(getResources().getColor(R.color.yellow_color_picker));
283
+ colorPickerColors.add(getResources().getColor(R.color.yellow_green_color_picker));
284
+ }
285
+
286
+ new CountDownTimer(500, 100) {
287
+ public void onTick(long millisUntilFinished) {}
288
+
289
+ public void onFinish() {
290
+ mLayout.setScrollableView(((ImageFragment) fragmentsList.get(0)).imageRecyclerView);
291
+ }
292
+ }
293
+ .start();
294
+
295
+ ArrayList hiddenControls = (ArrayList<Integer>) getIntent()
296
+ .getExtras()
297
+ .getSerializable("hiddenControls");
298
+ for (int i = 0; i < hiddenControls.size(); i++) {
299
+ if (hiddenControls.get(i).toString().equalsIgnoreCase("text")) {
300
+ addTextView.setVisibility(View.INVISIBLE);
301
+ }
302
+ if (hiddenControls.get(i).toString().equalsIgnoreCase("clear")) {
303
+ clearAllTextView.setVisibility(View.INVISIBLE);
304
+ }
305
+ if (hiddenControls.get(i).toString().equalsIgnoreCase("draw")) {
306
+ addPencil.setVisibility(View.INVISIBLE);
307
+ }
308
+ if (hiddenControls.get(i).toString().equalsIgnoreCase("save")) {
309
+ saveTextView.setVisibility(View.INVISIBLE);
310
+ }
311
+ if (hiddenControls.get(i).toString().equalsIgnoreCase("sticker")) {
312
+ addImageEmojiTextView.setVisibility(View.INVISIBLE);
313
+ }
314
+ if (hiddenControls.get(i).toString().equalsIgnoreCase("crop")) {
315
+ addCropTextView.setVisibility(View.INVISIBLE);
316
+ }
317
+ if (hiddenControls.get(i).toString().equalsIgnoreCase("rotate")) {
318
+ addRotateTextView.setVisibility(View.INVISIBLE);
319
+ }
320
+ }
321
+ }
322
+
323
+ private String getTranslation(String key) {
324
+ return TranslationService.getInstance().getTranslation(key);
325
+ }
326
+
327
+ private boolean stringIsNotEmpty(String string) {
328
+ if (string != null && !string.equals("null")) {
329
+ if (!string.trim().equals("")) {
330
+ return true;
331
+ }
332
+ }
333
+ return false;
334
+ }
335
+
336
+ public void addEmoji(String emojiName) {
337
+ photoEditorSDK.addEmoji(emojiName, emojiFont);
338
+ if (mLayout != null) mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
339
+ }
340
+
341
+ public void addImage(Bitmap image) {
342
+ photoEditorSDK.addImage(image);
343
+ if (mLayout != null) mLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED);
344
+ }
345
+
346
+ private void addText(String text, int colorCodeTextView) {
347
+ photoEditorSDK.addText(text, colorCodeTextView);
348
+ }
349
+
350
+ private void clearAllViews() {
351
+ photoEditorSDK.clearAllViews();
352
+ }
353
+
354
+ private void undoViews() {
355
+ photoEditorSDK.viewUndo();
356
+ }
357
+
358
+ private void eraseDrawing() {
359
+ photoEditorSDK.brushEraser();
360
+ }
361
+
362
+ private void openAddTextPopupWindow(String text, int colorCode) {
363
+ colorCodeTextView = colorCode;
364
+ LayoutInflater inflater = (LayoutInflater) getSystemService(
365
+ Context.LAYOUT_INFLATER_SERVICE
366
+ );
367
+ View addTextPopupWindowRootView = inflater.inflate(R.layout.add_text_popup_window, null);
368
+ final EditText addTextEditText = (EditText) addTextPopupWindowRootView.findViewById(
369
+ R.id.add_text_edit_text
370
+ );
371
+ addTextEditText.requestFocus();
372
+
373
+ TextView addTextDoneTextView = (TextView) addTextPopupWindowRootView.findViewById(
374
+ R.id.add_text_done_tv
375
+ );
376
+ addTextDoneTextView.setText(getTranslation("doneTitle"));
377
+ RecyclerView addTextColorPickerRecyclerView = (RecyclerView) addTextPopupWindowRootView.findViewById(
378
+ R.id.add_text_color_picker_recycler_view
379
+ );
380
+ LinearLayoutManager layoutManager = new LinearLayoutManager(
381
+ PhotoEditorActivity.this,
382
+ LinearLayoutManager.HORIZONTAL,
383
+ false
384
+ );
385
+ addTextColorPickerRecyclerView.setLayoutManager(layoutManager);
386
+ addTextColorPickerRecyclerView.setHasFixedSize(true);
387
+ ColorPickerAdapter colorPickerAdapter = new ColorPickerAdapter(
388
+ PhotoEditorActivity.this,
389
+ colorPickerColors
390
+ );
391
+ colorPickerAdapter.setOnColorPickerClickListener(
392
+ new ColorPickerAdapter.OnColorPickerClickListener() {
393
+ @Override
394
+ public void onColorPickerClickListener(int colorCode) {
395
+ addTextEditText.setTextColor(colorCode);
396
+ colorCodeTextView = colorCode;
397
+ }
398
+ }
399
+ );
400
+ addTextColorPickerRecyclerView.setAdapter(colorPickerAdapter);
401
+ if (stringIsNotEmpty(text)) {
402
+ addTextEditText.setText(text);
403
+ addTextEditText.setTextColor(
404
+ colorCode == -1 ? getResources().getColor(R.color.white) : colorCode
405
+ );
406
+ }
407
+ final PopupWindow pop = new PopupWindow(PhotoEditorActivity.this);
408
+ pop.setContentView(addTextPopupWindowRootView);
409
+ pop.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
410
+ pop.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
411
+ pop.setFocusable(true);
412
+ pop.setBackgroundDrawable(null);
413
+ pop.showAtLocation(addTextPopupWindowRootView, Gravity.TOP, 0, 0);
414
+ InputMethodManager imm = (InputMethodManager) getSystemService(
415
+ Context.INPUT_METHOD_SERVICE
416
+ );
417
+ imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
418
+ addTextDoneTextView.setOnClickListener(
419
+ new View.OnClickListener() {
420
+ @Override
421
+ public void onClick(View view) {
422
+ addText(addTextEditText.getText().toString(), colorCodeTextView);
423
+ InputMethodManager imm = (InputMethodManager) getSystemService(
424
+ Context.INPUT_METHOD_SERVICE
425
+ );
426
+ imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
427
+ pop.dismiss();
428
+ }
429
+ }
430
+ );
431
+ }
432
+
433
+ private void updateView(int visibility) {
434
+ topShadow.setVisibility(visibility);
435
+ topShadowRelativeLayout.setVisibility(visibility);
436
+ bottomShadow.setVisibility(visibility);
437
+ bottomShadowRelativeLayout.setVisibility(visibility);
438
+ }
439
+
440
+ private void updateBrushDrawingView(boolean brushDrawingMode) {
441
+ photoEditorSDK.setBrushDrawingMode(brushDrawingMode);
442
+ if (brushDrawingMode) {
443
+ updateView(View.GONE);
444
+ drawingViewColorPickerRecyclerView.setVisibility(View.VISIBLE);
445
+ doneDrawingTextView.setVisibility(View.VISIBLE);
446
+ eraseDrawingTextView.setVisibility(View.VISIBLE);
447
+ LinearLayoutManager layoutManager = new LinearLayoutManager(
448
+ PhotoEditorActivity.this,
449
+ LinearLayoutManager.HORIZONTAL,
450
+ false
451
+ );
452
+ drawingViewColorPickerRecyclerView.setLayoutManager(layoutManager);
453
+ drawingViewColorPickerRecyclerView.setHasFixedSize(true);
454
+ ColorPickerAdapter colorPickerAdapter = new ColorPickerAdapter(
455
+ PhotoEditorActivity.this,
456
+ colorPickerColors
457
+ );
458
+ colorPickerAdapter.setOnColorPickerClickListener(
459
+ new ColorPickerAdapter.OnColorPickerClickListener() {
460
+ @Override
461
+ public void onColorPickerClickListener(int colorCode) {
462
+ photoEditorSDK.setBrushColor(colorCode);
463
+ }
464
+ }
465
+ );
466
+ drawingViewColorPickerRecyclerView.setAdapter(colorPickerAdapter);
467
+ } else {
468
+ updateView(View.VISIBLE);
469
+ drawingViewColorPickerRecyclerView.setVisibility(View.GONE);
470
+ doneDrawingTextView.setVisibility(View.GONE);
471
+ eraseDrawingTextView.setVisibility(View.GONE);
472
+ }
473
+ }
474
+
475
+ private void returnBackWithSavedImage() {
476
+ if (shouldRequestWritePermission()) {
477
+ showPermissionRequest();
478
+ } else {
479
+ saveImageAndFinish();
480
+ }
481
+ }
482
+
483
+ private boolean shouldRequestWritePermission() {
484
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
485
+ int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(
486
+ this,
487
+ android.Manifest.permission.WRITE_EXTERNAL_STORAGE
488
+ );
489
+ return permissionCheck != PackageManager.PERMISSION_GRANTED;
490
+ }
491
+ return false;
492
+ }
493
+
494
+ private void saveImageAndFinish() {
495
+ updateView(View.GONE);
496
+ RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
497
+ RelativeLayout.LayoutParams.WRAP_CONTENT,
498
+ RelativeLayout.LayoutParams.WRAP_CONTENT
499
+ );
500
+ layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
501
+ parentImageRelativeLayout.setLayoutParams(layoutParams);
502
+ new CountDownTimer(1000, 500) {
503
+ public void onTick(long millisUntilFinished) {}
504
+
505
+ public void onFinish() {
506
+ String imagePath = saveImageToExternalStorage();
507
+ Intent returnIntent = new Intent();
508
+ returnIntent.putExtra("imagePath", imagePath);
509
+ setResult(Activity.RESULT_OK, returnIntent);
510
+ finish();
511
+ Toast
512
+ .makeText(
513
+ PhotoEditorActivity.this,
514
+ getString(R.string.save_image_succeed),
515
+ Toast.LENGTH_SHORT
516
+ )
517
+ .show();
518
+ }
519
+ }
520
+ .start();
521
+ }
522
+
523
+ private String saveImageToExternalStorage() {
524
+ String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
525
+ String imageName = "IMG_" + timeStamp + ".jpg";
526
+ String selectedOutputPath = "";
527
+
528
+ if (isSDCARDMounted()) {
529
+ String folderName = "PhotoEditorSDK";
530
+ File mediaStorageDir = new File(
531
+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
532
+ folderName
533
+ );
534
+ if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()) {
535
+ Log.d("PhotoEditorSDK", "Failed to create directory");
536
+ }
537
+
538
+ selectedOutputPath = mediaStorageDir.getPath() + File.separator + imageName;
539
+ Log.d("PhotoEditorSDK", "selected camera path " + selectedOutputPath);
540
+ File file = new File(selectedOutputPath);
541
+
542
+ try {
543
+ FileOutputStream out = new FileOutputStream(file);
544
+ if (parentImageRelativeLayout != null) {
545
+ parentImageRelativeLayout.setDrawingCacheEnabled(true);
546
+
547
+ Bitmap bitmap = parentImageRelativeLayout.getDrawingCache();
548
+ Bitmap rotatedBitmap = rotateBitmap(bitmap, imageOrientation, true);
549
+ rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
550
+ }
551
+
552
+ out.flush();
553
+ out.close();
554
+
555
+ try {
556
+ ExifInterface exifDest = new ExifInterface(file.getAbsolutePath());
557
+ exifDest.setAttribute(
558
+ ExifInterface.TAG_ORIENTATION,
559
+ Integer.toString(imageOrientation)
560
+ );
561
+ exifDest.saveAttributes();
562
+ } catch (IOException e) {
563
+ e.printStackTrace();
564
+ }
565
+ } catch (Exception var7) {
566
+ var7.printStackTrace();
567
+ }
568
+ }
569
+
570
+ return selectedOutputPath;
571
+ }
572
+
573
+ private void returnBackWithUpdateImage() {
574
+ overlay.setVisibility(View.VISIBLE);
575
+
576
+ new CountDownTimer(1000, 500) {
577
+ public void onTick(long millisUntilFinished) {}
578
+
579
+ public void onFinish() {
580
+ String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
581
+ String imageName = "/IMG_" + timeStamp + ".jpg";
582
+
583
+ String selectedImagePath = getIntent().getExtras().getString("selectedImagePath");
584
+ File file = new File(selectedImagePath);
585
+
586
+ try {
587
+ FileOutputStream out = new FileOutputStream(file);
588
+ if (parentImageRelativeLayout != null) {
589
+ parentImageRelativeLayout.setDrawingCacheEnabled(true);
590
+ Bitmap bitmap = parentImageRelativeLayout.getDrawingCache();
591
+ Bitmap rotatedBitmap = rotateBitmap(bitmap, imageOrientation, true);
592
+ rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
593
+ }
594
+
595
+ out.flush();
596
+ out.close();
597
+ try {
598
+ ExifInterface exifDest = new ExifInterface(file.getAbsolutePath());
599
+ exifDest.setAttribute(
600
+ ExifInterface.TAG_ORIENTATION,
601
+ Integer.toString(imageOrientation)
602
+ );
603
+ exifDest.saveAttributes();
604
+ } catch (IOException e) {
605
+ e.printStackTrace();
606
+ }
607
+ } catch (Exception var7) {
608
+ var7.printStackTrace();
609
+ }
610
+
611
+ Intent returnIntent = new Intent();
612
+ returnIntent.putExtra("imagePath", selectedImagePath);
613
+ setResult(Activity.RESULT_OK, returnIntent);
614
+ overlay.setVisibility(View.GONE);
615
+
616
+ finish();
617
+ }
618
+ }
619
+ .start();
620
+ }
621
+
622
+ private boolean isSDCARDMounted() {
623
+ String status = Environment.getExternalStorageState();
624
+ return status.equals("mounted");
625
+ }
626
+
627
+ public void showPermissionRequest() {
628
+ AlertDialog.Builder builder = new AlertDialog.Builder(this);
629
+ builder.setMessage(getString(R.string.access_media_permissions_msg));
630
+ builder.setPositiveButton(
631
+ getString(R.string.continue_txt),
632
+ new DialogInterface.OnClickListener() {
633
+ @Override
634
+ public void onClick(DialogInterface dialogInterface, int i) {
635
+ ActivityCompat.requestPermissions(
636
+ PhotoEditorActivity.this,
637
+ new String[] { android.Manifest.permission.WRITE_EXTERNAL_STORAGE },
638
+ MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE_GALLERY
639
+ );
640
+ }
641
+ }
642
+ );
643
+ builder.setNegativeButton(
644
+ getString(R.string.not_now),
645
+ new DialogInterface.OnClickListener() {
646
+ @Override
647
+ public void onClick(DialogInterface dialogInterface, int i) {
648
+ Toast
649
+ .makeText(
650
+ PhotoEditorActivity.this,
651
+ getString(R.string.media_access_denied_msg),
652
+ Toast.LENGTH_SHORT
653
+ )
654
+ .show();
655
+ }
656
+ }
657
+ );
658
+ builder.show();
659
+ }
660
+
661
+ @Override
662
+ public void onRequestPermissionsResult(
663
+ int requestCode,
664
+ @NonNull String[] permissions,
665
+ @NonNull int[] grantResults
666
+ ) {
667
+ if (requestCode == MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE_GALLERY) {
668
+ // If request is cancelled, the result arrays are empty.
669
+ int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(
670
+ this,
671
+ android.Manifest.permission.WRITE_EXTERNAL_STORAGE
672
+ );
673
+ if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
674
+ returnBackWithSavedImage();
675
+ } else {
676
+ // permission denied, boo! Disable the
677
+ // functionality that depends on this permission.
678
+ Toast
679
+ .makeText(this, getString(R.string.media_access_denied_msg), Toast.LENGTH_SHORT)
680
+ .show();
681
+ }
682
+ return;
683
+ }
684
+ }
685
+
686
+ @Override
687
+ public void onClick(View v) {
688
+ if (v.getId() == R.id.close_tv) {
689
+ onBackPressed();
690
+ } else if (v.getId() == R.id.add_image_emoji_tv) {
691
+ mLayout.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
692
+ } else if (v.getId() == R.id.add_crop_tv) {
693
+ System.out.println("CROP IMAGE DUD");
694
+ startCropping();
695
+ } else if (v.getId() == R.id.add_rotate_tv) {
696
+ System.out.println("Rotate IMAGE DUD");
697
+ startRotate();
698
+ } else if (v.getId() == R.id.add_text_tv) {
699
+ openAddTextPopupWindow("", -1);
700
+ } else if (v.getId() == R.id.add_pencil_tv) {
701
+ updateBrushDrawingView(true);
702
+ } else if (v.getId() == R.id.done_drawing_tv) {
703
+ updateBrushDrawingView(false);
704
+ } else if (v.getId() == R.id.save_tv) {
705
+ returnBackWithSavedImage();
706
+ } else if (v.getId() == R.id.clear_all_tv) {
707
+ clearAllViews();
708
+ } else if (v.getId() == R.id.undo_text_tv || v.getId() == R.id.undo_tv) {
709
+ undoViews();
710
+ } else if (v.getId() == R.id.erase_drawing_tv) {
711
+ eraseDrawing();
712
+ } else if (v.getId() == R.id.go_to_next_screen_tv) {
713
+ returnBackWithUpdateImage();
714
+ }
715
+ }
716
+
717
+ @Override
718
+ public void onEditTextChangeListener(String text, int colorCode) {
719
+ openAddTextPopupWindow(text, colorCode);
720
+ }
721
+
722
+ @Override
723
+ public void onAddViewListener(ViewType viewType, int numberOfAddedViews) {
724
+ if (numberOfAddedViews > 0) {
725
+ undoTextView.setVisibility(View.VISIBLE);
726
+ undoTextTextView.setVisibility(View.VISIBLE);
727
+ }
728
+ switch (viewType) {
729
+ case BRUSH_DRAWING:
730
+ Log.i("BRUSH_DRAWING", "onAddViewListener");
731
+ break;
732
+ case EMOJI:
733
+ Log.i("EMOJI", "onAddViewListener");
734
+ break;
735
+ case IMAGE:
736
+ Log.i("IMAGE", "onAddViewListener");
737
+ break;
738
+ case TEXT:
739
+ Log.i("TEXT", "onAddViewListener");
740
+ break;
741
+ }
742
+ }
743
+
744
+ @Override
745
+ public void onRemoveViewListener(int numberOfAddedViews) {
746
+ Log.i(TAG, "onRemoveViewListener");
747
+ if (numberOfAddedViews == 0) {
748
+ undoTextView.setVisibility(View.GONE);
749
+ undoTextTextView.setVisibility(View.GONE);
750
+ }
751
+ }
752
+
753
+ @Override
754
+ public void onStartViewChangeListener(ViewType viewType) {
755
+ switch (viewType) {
756
+ case BRUSH_DRAWING:
757
+ Log.i("BRUSH_DRAWING", "onStartViewChangeListener");
758
+ break;
759
+ case EMOJI:
760
+ Log.i("EMOJI", "onStartViewChangeListener");
761
+ break;
762
+ case IMAGE:
763
+ Log.i("IMAGE", "onStartViewChangeListener");
764
+ break;
765
+ case TEXT:
766
+ Log.i("TEXT", "onStartViewChangeListener");
767
+ break;
768
+ }
769
+ }
770
+
771
+ @Override
772
+ public void onStopViewChangeListener(ViewType viewType) {
773
+ switch (viewType) {
774
+ case BRUSH_DRAWING:
775
+ Log.i("BRUSH_DRAWING", "onStopViewChangeListener");
776
+ break;
777
+ case EMOJI:
778
+ Log.i("EMOJI", "onStopViewChangeListener");
779
+ break;
780
+ case IMAGE:
781
+ Log.i("IMAGE", "onStopViewChangeListener");
782
+ break;
783
+ case TEXT:
784
+ Log.i("TEXT", "onStopViewChangeListener");
785
+ break;
786
+ }
787
+ }
788
+
789
+ private class PreviewSlidePagerAdapter extends FragmentStatePagerAdapter {
790
+
791
+ private List<Fragment> mFragments;
792
+
793
+ PreviewSlidePagerAdapter(FragmentManager fm, List<Fragment> fragments) {
794
+ super(fm);
795
+ mFragments = fragments;
796
+ }
797
+
798
+ @Override
799
+ public Fragment getItem(int position) {
800
+ if (mFragments == null) {
801
+ return (null);
802
+ }
803
+ return mFragments.get(position);
804
+ }
805
+
806
+ @Override
807
+ public int getCount() {
808
+ return 2;
809
+ }
810
+ }
811
+
812
+ private Typeface getFontFromRes(int resource) {
813
+ Typeface tf = null;
814
+ InputStream is = null;
815
+ try {
816
+ is = getResources().openRawResource(resource);
817
+ } catch (Resources.NotFoundException e) {
818
+ Log.e(TAG, "Could not find font in resources!");
819
+ }
820
+
821
+ String outPath = getCacheDir() + "/tmp" + System.currentTimeMillis() + ".raw";
822
+
823
+ try {
824
+ byte[] buffer = new byte[is.available()];
825
+ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outPath));
826
+
827
+ int l = 0;
828
+ while ((l = is.read(buffer)) > 0) bos.write(buffer, 0, l);
829
+
830
+ bos.close();
831
+
832
+ tf = Typeface.createFromFile(outPath);
833
+
834
+ // clean up
835
+ new File(outPath).delete();
836
+ } catch (IOException e) {
837
+ Log.e(TAG, "Error reading in font!");
838
+ return null;
839
+ }
840
+
841
+ Log.d(TAG, "Successfully loaded font.");
842
+
843
+ return tf;
844
+ }
845
+
846
+ private void startCropping() {
847
+ System.out.println(selectedImagePath);
848
+ Uri uri = Uri.fromFile(new File(selectedImagePath));
849
+ UCrop.Options options = new UCrop.Options();
850
+ options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
851
+ options.setCompressionQuality(100);
852
+ options.setCircleDimmedLayer(cropperCircleOverlay);
853
+ options.setFreeStyleCropEnabled(freeStyleCropEnabled);
854
+ options.setShowCropGrid(showCropGuidelines);
855
+ options.setHideBottomControls(hideBottomControls);
856
+ options.setAllowedGestures(
857
+ UCropActivity.ALL, // When 'scale'-tab active
858
+ UCropActivity.ALL, // When 'rotate'-tab active
859
+ UCropActivity.ALL // When 'aspect ratio'-tab active
860
+ );
861
+
862
+ UCrop uCrop = UCrop
863
+ .of(
864
+ uri,
865
+ Uri.fromFile(new File(this.getTmpDir(this), UUID.randomUUID().toString() + ".jpg"))
866
+ )
867
+ .withOptions(options);
868
+
869
+ uCrop.start(this);
870
+ }
871
+
872
+ private void startRotate() {
873
+ // Get the current bitmap from the ImageView
874
+ BitmapDrawable drawable = (BitmapDrawable) photoEditImageView.getDrawable();
875
+ Bitmap currentBitmap = drawable.getBitmap();
876
+
877
+ Matrix matrix = new Matrix();
878
+ matrix.postRotate(90);
879
+ Bitmap rotatedBitmap = Bitmap.createBitmap(
880
+ currentBitmap,
881
+ 0,
882
+ 0,
883
+ currentBitmap.getWidth(),
884
+ currentBitmap.getHeight(),
885
+ matrix,
886
+ true
887
+ );
888
+
889
+ // Set the rotated bitmap on the ImageView
890
+ photoEditImageView.setImageBitmap(rotatedBitmap);
891
+ }
892
+
893
+ private String getTmpDir(Activity activity) {
894
+ String tmpDir = activity.getCacheDir() + "/react-native-image-editor";
895
+ new File(tmpDir).mkdir();
896
+
897
+ return tmpDir;
898
+ }
899
+
900
+ @Override
901
+ public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
902
+ if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
903
+ if (data != null) {
904
+ final Uri resultUri = UCrop.getOutput(data);
905
+ if (resultUri != null) {
906
+ try {
907
+ selectedImagePath = resultUri.toString();
908
+ Bitmap bitmap = MediaStore.Images.Media.getBitmap(
909
+ this.getContentResolver(),
910
+ resultUri
911
+ );
912
+ photoEditImageView.setImageBitmap(bitmap);
913
+ } catch (Exception ex) {
914
+ System.out.println("NO IMAGE DATA FOUND");
915
+ }
916
+ } else {
917
+ System.out.println("NO IMAGE DATA FOUND");
918
+ }
919
+ } else {
920
+ System.out.println("NO RESULT");
921
+ }
922
+ }
923
+ }
924
+
925
+ @TargetApi(Build.VERSION_CODES.KITKAT)
926
+ protected String getPath(final Uri uri) {
927
+ // DocumentProvider
928
+ if (isKitKat && DocumentsContract.isDocumentUri(this, uri)) {
929
+ // ExternalStorageProvider
930
+ if (GalleryUtils.isExternalStorageDocument(uri)) {
931
+ final String docId = DocumentsContract.getDocumentId(uri);
932
+ final String[] split = docId.split(":");
933
+ final String type = split[0];
934
+ if ("primary".equalsIgnoreCase(type)) {
935
+ return Environment.getExternalStorageDirectory() + "/" + split[1];
936
+ }
937
+ }
938
+ // DownloadsProvider
939
+ else if (GalleryUtils.isDownloadsDocument(uri)) {
940
+ final String id = DocumentsContract.getDocumentId(uri);
941
+ final Uri contentUri = ContentUris.withAppendedId(
942
+ Uri.parse("content://downloads/public_downloads"),
943
+ Long.valueOf(id)
944
+ );
945
+ return GalleryUtils.getDataColumn(this, contentUri, null, null);
946
+ }
947
+ // MediaProvider
948
+ else if (GalleryUtils.isMediaDocument(uri)) {
949
+ final String docId = DocumentsContract.getDocumentId(uri);
950
+ final String[] split = docId.split(":");
951
+ final String type = split[0];
952
+
953
+ Uri contentUri = null;
954
+ if ("image".equals(type)) {
955
+ contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
956
+ } else if ("video".equals(type)) {
957
+ contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
958
+ } else if ("audio".equals(type)) {
959
+ contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
960
+ }
961
+
962
+ final String selection = "_id=?";
963
+ final String[] selectionArgs = new String[] { split[1] };
964
+
965
+ return GalleryUtils.getDataColumn(this, contentUri, selection, selectionArgs);
966
+ }
967
+ }
968
+ // MediaStore (and general)
969
+ else if ("content".equalsIgnoreCase(uri.getScheme())) {
970
+ return GalleryUtils.getDataColumn(this, uri, null, null);
971
+ }
972
+ // File
973
+ else if ("file".equalsIgnoreCase(uri.getScheme())) {
974
+ return uri.getPath();
975
+ }
976
+
977
+ return null;
978
+ }
979
+
980
+ private static Bitmap rotateBitmap(Bitmap bitmap, int orientation, boolean reverse) {
981
+ Matrix matrix = new Matrix();
982
+
983
+ switch (orientation) {
984
+ case ExifInterface.ORIENTATION_NORMAL:
985
+ return bitmap;
986
+ case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
987
+ matrix.setScale(-1, 1);
988
+
989
+ break;
990
+ case ExifInterface.ORIENTATION_ROTATE_180:
991
+ matrix.setRotate(180);
992
+
993
+ break;
994
+ case ExifInterface.ORIENTATION_FLIP_VERTICAL:
995
+ matrix.setRotate(180);
996
+ matrix.postScale(-1, 1);
997
+
998
+ break;
999
+ case ExifInterface.ORIENTATION_TRANSPOSE:
1000
+ if (!reverse) {
1001
+ matrix.setRotate(90);
1002
+ } else {
1003
+ matrix.setRotate(-90);
1004
+ }
1005
+
1006
+ matrix.postScale(-1, 1);
1007
+ break;
1008
+ case ExifInterface.ORIENTATION_ROTATE_90:
1009
+ if (!reverse) {
1010
+ matrix.setRotate(90);
1011
+ } else {
1012
+ matrix.setRotate(-90);
1013
+ }
1014
+
1015
+ break;
1016
+ case ExifInterface.ORIENTATION_TRANSVERSE:
1017
+ if (!reverse) {
1018
+ matrix.setRotate(-90);
1019
+ } else {
1020
+ matrix.setRotate(90);
1021
+ }
1022
+
1023
+ matrix.postScale(-1, 1);
1024
+ break;
1025
+ case ExifInterface.ORIENTATION_ROTATE_270:
1026
+ if (!reverse) {
1027
+ matrix.setRotate(-90);
1028
+ } else {
1029
+ matrix.setRotate(90);
1030
+ }
1031
+
1032
+ break;
1033
+ default:
1034
+ return bitmap;
1035
+ }
1036
+
1037
+ try {
1038
+ Bitmap bmRotated = Bitmap.createBitmap(
1039
+ bitmap,
1040
+ 0,
1041
+ 0,
1042
+ bitmap.getWidth(),
1043
+ bitmap.getHeight(),
1044
+ matrix,
1045
+ true
1046
+ );
1047
+ bitmap.recycle();
1048
+
1049
+ return bmRotated;
1050
+ } catch (OutOfMemoryError e) {
1051
+ e.printStackTrace();
1052
+ return null;
1053
+ }
1054
+ }
1055
+ }