5
5
import android .content .ClipData ;
6
6
import android .content .Intent ;
7
7
import android .content .pm .PackageManager ;
8
+ import android .database .Cursor ;
8
9
import android .net .Uri ;
9
10
import android .os .Build ;
10
11
import android .os .Bundle ;
12
+ import android .os .Parcelable ;
11
13
import android .provider .MediaStore ;
12
14
import android .util .Log ;
13
15
14
16
import androidx .core .app .ActivityCompat ;
15
17
import androidx .core .content .ContextCompat ;
16
18
19
+ import com .shz .imagepicker .imagepicker .util .FileOrientationHandler ;
20
+
17
21
import java .io .File ;
22
+ import java .io .IOException ;
23
+ import java .net .URI ;
18
24
import java .util .ArrayList ;
19
25
20
26
public class GalleryMultiPickerActivity extends Activity {
21
27
22
28
public static ImagePickerCallback mCallback ;
23
29
24
30
private static final int PERMISSION_REQUEST_READ_STORAGE = 54564 ;
25
- private static final int IMAGE_REQUEST_GALLERY = 54565 ;
31
+ private static final int IMAGE_REQUEST_GALLERY = 1 ;
26
32
27
33
private static final String GALLERY_IMAGE_MIME = "image/jpeg" ;
34
+ private FileOrientationHandler fileOrientationHandler = new FileOrientationHandler ();
28
35
29
36
@ Override
30
37
public void onRequestPermissionsResult (int requestCode , String [] permissions , int [] grantResults ) {
@@ -48,17 +55,34 @@ protected void onCreate(Bundle savedInstanceState) {
48
55
protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
49
56
super .onActivityResult (requestCode , resultCode , data );
50
57
if (resultCode == Activity .RESULT_OK && requestCode == IMAGE_REQUEST_GALLERY ) {
51
- if (data != null && data .getData () != null ) {
58
+
59
+
60
+ if (data != null ) {
52
61
ArrayList <File > files = new ArrayList <>();
53
62
54
- /*if (data.getData() != null) {
63
+
64
+ if (data .getExtras () != null ) {
65
+ ArrayList <Parcelable > fileUris = data .getExtras ().getParcelableArrayList ("selectedItems" );
66
+
67
+ for (Parcelable uri : fileUris ) {
68
+ if (uri instanceof Uri ) {
69
+ files .add (new File (getRealPathFromURI ((Uri ) uri )));
70
+ }
71
+
72
+ }
73
+
74
+
75
+ }
76
+
77
+ if (data .getData () != null ) {
55
78
Log .d ("MultiPicker" , "data: " + data .getData ().toString ());
56
79
String filename = ImagePath .getImagePathFromInputStreamUri (this , data .getData ());
57
80
File file = new File (filename );
58
81
if (file .exists ()) {
59
82
files .add (file );
60
83
}
61
- }*/
84
+ }
85
+
62
86
if (data .getClipData () != null ) {
63
87
ClipData clipData = data .getClipData ();
64
88
@@ -72,18 +96,41 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
72
96
}
73
97
}
74
98
99
+
100
+ try {
101
+ for (File file :
102
+ files ) {
103
+ fileOrientationHandler .rotateAndReWriteImageFile (file );
104
+ }
105
+ } catch (IOException e ) {
106
+ Log .d (this .getClass ().getSimpleName (), "Error rotating image " + e .getLocalizedMessage ());
107
+ }
108
+
109
+
75
110
mCallback .onImagesSelected (files );
76
111
}
77
112
}
78
113
finish ();
79
114
}
80
115
116
+ public String getRealPathFromURI (Uri contentUri ) {
117
+ String path = null ;
118
+ String [] proj = {MediaStore .MediaColumns .DATA };
119
+ Cursor cursor = getContentResolver ().query (contentUri , proj , null , null , null );
120
+ if (cursor .moveToFirst ()) {
121
+ int column_index = cursor .getColumnIndexOrThrow (MediaStore .MediaColumns .DATA );
122
+ path = cursor .getString (column_index );
123
+ }
124
+ cursor .close ();
125
+ return path ;
126
+ }
127
+
81
128
private void checkGalleryPermission () {
82
129
if (ContextCompat .checkSelfPermission (this , Manifest .permission .READ_EXTERNAL_STORAGE ) == PackageManager .PERMISSION_DENIED
83
130
|| ContextCompat .checkSelfPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE ) == PackageManager .PERMISSION_DENIED ) {
84
131
ActivityCompat .requestPermissions (
85
132
this ,
86
- new String [] { Manifest .permission .READ_EXTERNAL_STORAGE , Manifest .permission .WRITE_EXTERNAL_STORAGE },
133
+ new String []{ Manifest .permission .READ_EXTERNAL_STORAGE , Manifest .permission .WRITE_EXTERNAL_STORAGE },
87
134
PERMISSION_REQUEST_READ_STORAGE
88
135
);
89
136
} else {
@@ -93,10 +140,15 @@ private void checkGalleryPermission() {
93
140
94
141
private void startGalleryPicker () {
95
142
Intent galleryIntent = new Intent (Intent .ACTION_PICK , MediaStore .Images .Media .EXTERNAL_CONTENT_URI );
96
- if (Build .VERSION .SDK_INT >= 18 ) {
143
+
144
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR2 ) {
97
145
galleryIntent .putExtra (Intent .EXTRA_ALLOW_MULTIPLE , true );
98
146
}
99
- galleryIntent .setDataAndType (MediaStore .Images .Media .EXTERNAL_CONTENT_URI , GALLERY_IMAGE_MIME );
147
+ galleryIntent .putExtra ("multi-pick" , true );
148
+
149
+
150
+ //galleryIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, GALLERY_IMAGE_MIME);
100
151
startActivityForResult (galleryIntent , IMAGE_REQUEST_GALLERY );
152
+
101
153
}
102
154
}
0 commit comments