17
17
18
18
package com .naver .android .helloyako .imagecropsample ;
19
19
20
+ import android .Manifest ;
20
21
import android .app .Activity ;
21
22
import android .app .ProgressDialog ;
22
23
import android .content .DialogInterface ;
23
24
import android .content .Intent ;
25
+ import android .content .pm .PackageManager ;
24
26
import android .database .Cursor ;
25
27
import android .graphics .Bitmap ;
26
28
import android .graphics .drawable .BitmapDrawable ;
27
29
import android .graphics .drawable .Drawable ;
28
30
import android .net .Uri ;
29
31
import android .os .AsyncTask ;
32
+ import android .os .Build ;
30
33
import android .os .Bundle ;
31
34
import android .provider .MediaStore ;
35
+ import android .support .annotation .NonNull ;
32
36
import android .util .Log ;
33
37
import android .view .View ;
34
38
import android .widget .Button ;
@@ -42,6 +46,7 @@ public class MainActivity extends Activity {
42
46
43
47
public static final String TAG = "MainActivity" ;
44
48
49
+ private static final int MAIN_ACTIVITY_REQUEST_STORAGE = RESULT_FIRST_USER ;
45
50
private static final int ACTION_REQUEST_GALLERY = 99 ;
46
51
47
52
Button mGalleryButton ;
@@ -61,117 +66,140 @@ protected void onCreate(Bundle savedInstanceState) {
61
66
imageWidth = 1000 ;
62
67
imageHeight = 1000 ;
63
68
64
- mGalleryButton = (Button ) findViewById ( R .id .button1 );
65
- mEditButton = (Button ) findViewById ( R .id .button2 );
66
- mImage = ( (ImageView ) findViewById ( R .id .image ) );
67
- mImageContainer = findViewById ( R .id .image_container );
69
+ mGalleryButton = (Button ) findViewById (R .id .button1 );
70
+ mEditButton = (Button ) findViewById (R .id .button2 );
71
+ mImage = ((ImageView ) findViewById (R .id .image ) );
72
+ mImageContainer = findViewById (R .id .image_container );
68
73
69
- mGalleryButton .setOnClickListener ( new View .OnClickListener () {
74
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
75
+ if (getPackageManager ().checkPermission (Manifest .permission .WRITE_EXTERNAL_STORAGE , getPackageName ()) == PackageManager .PERMISSION_GRANTED ) {
76
+ initClickListener ();
77
+ }
78
+ requestPermissions (new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE }, MAIN_ACTIVITY_REQUEST_STORAGE );
79
+ } else {
80
+ initClickListener ();
81
+ }
82
+ }
83
+
84
+ @ Override
85
+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
86
+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
87
+ switch (requestCode ) {
88
+ case MAIN_ACTIVITY_REQUEST_STORAGE :
89
+ if (grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
90
+ initClickListener ();
91
+ }
92
+ break ;
93
+ }
94
+ }
95
+
96
+ private void initClickListener () {
97
+ mGalleryButton .setOnClickListener (new View .OnClickListener () {
70
98
71
99
@ Override
72
- public void onClick ( View v ) {
100
+ public void onClick (View v ) {
73
101
pickFromGallery ();
74
102
}
75
- } );
103
+ });
76
104
77
- mEditButton .setOnClickListener ( new View .OnClickListener () {
105
+ mEditButton .setOnClickListener (new View .OnClickListener () {
78
106
79
107
@ Override
80
- public void onClick ( View v ) {
81
- if ( mImageUri != null ) {
108
+ public void onClick (View v ) {
109
+ if (mImageUri != null ) {
82
110
startCrop (mImageUri );
83
111
}
84
112
}
85
- } );
113
+ });
86
114
87
- mImageContainer .setOnClickListener ( new View .OnClickListener () {
115
+ mImageContainer .setOnClickListener (new View .OnClickListener () {
88
116
89
117
@ Override
90
- public void onClick ( View v ) {
118
+ public void onClick (View v ) {
91
119
findViewById (R .id .touch_me ).setVisibility (View .GONE );
92
120
Uri uri = pickRandomImage ();
93
- if ( uri != null ) {
121
+ if (uri != null ) {
94
122
Log .d (TAG , "image uri: " + uri );
95
- loadAsync ( uri );
123
+ loadAsync (uri );
96
124
}
97
125
}
98
- } );
126
+ });
99
127
}
100
128
101
129
@ Override
102
130
protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
103
- if ( resultCode == RESULT_OK ) {
131
+ if (resultCode == RESULT_OK ) {
104
132
switch (requestCode ) {
105
133
case ACTION_REQUEST_GALLERY :
106
134
Uri uri = data .getData ();
107
135
String filePath = BitmapLoadUtils .getPathFromUri (this , uri );
108
136
Uri filePathUri = Uri .parse (filePath );
109
- loadAsync ( filePathUri );
137
+ loadAsync (filePathUri );
110
138
break ;
111
139
}
112
140
}
113
141
}
114
142
115
143
private void pickFromGallery () {
116
- Intent intent = new Intent ( Intent .ACTION_GET_CONTENT );
117
- intent .setType ( "image/*" );
144
+ Intent intent = new Intent (Intent .ACTION_GET_CONTENT );
145
+ intent .setType ("image/*" );
118
146
119
- Intent chooser = Intent .createChooser ( intent , "Choose a Picture" );
120
- startActivityForResult ( chooser , ACTION_REQUEST_GALLERY );
147
+ Intent chooser = Intent .createChooser (intent , "Choose a Picture" );
148
+ startActivityForResult (chooser , ACTION_REQUEST_GALLERY );
121
149
}
122
150
123
- private void startCrop (Uri imageUri ){
124
- Intent intent = new Intent (MainActivity .this ,CropActivity .class );
151
+ private void startCrop (Uri imageUri ) {
152
+ Intent intent = new Intent (MainActivity .this , CropActivity .class );
125
153
intent .setData (imageUri );
126
154
startActivity (intent );
127
155
}
128
156
129
- private boolean setImageURI ( final Uri uri , final Bitmap bitmap ) {
157
+ private boolean setImageURI (final Uri uri , final Bitmap bitmap ) {
130
158
131
- Log .d ( TAG , "image size: " + bitmap .getWidth () + "x" + bitmap .getHeight () );
132
- mImage .setImageBitmap ( bitmap );
133
- mImage .setBackgroundDrawable ( null );
159
+ Log .d (TAG , "image size: " + bitmap .getWidth () + "x" + bitmap .getHeight ());
160
+ mImage .setImageBitmap (bitmap );
161
+ mImage .setBackgroundDrawable (null );
134
162
135
- mEditButton .setEnabled ( true );
163
+ mEditButton .setEnabled (true );
136
164
mImageUri = uri ;
137
165
138
166
return true ;
139
167
}
140
168
141
169
private Uri pickRandomImage () {
142
- Cursor c = getContentResolver ().query ( MediaStore .Images .Media .EXTERNAL_CONTENT_URI , new String [] { MediaStore .Images .ImageColumns ._ID , MediaStore .Images .ImageColumns .DATA },
143
- MediaStore .Images .ImageColumns .SIZE + ">?" , new String [] { "90000" }, null );
170
+ Cursor c = getContentResolver ().query (MediaStore .Images .Media .EXTERNAL_CONTENT_URI , new String []{ MediaStore .Images .ImageColumns ._ID , MediaStore .Images .ImageColumns .DATA },
171
+ MediaStore .Images .ImageColumns .SIZE + ">?" , new String []{ "90000" }, null );
144
172
Uri uri = null ;
145
173
146
- if ( c != null ) {
174
+ if (c != null ) {
147
175
int total = c .getCount ();
148
- int position = (int ) ( Math .random () * total );
149
- Log .d ( TAG , "pickRandomImage. total images: " + total + ", position: " + position );
150
- if ( total > 0 ) {
151
- if ( c .moveToPosition ( position ) ) {
176
+ int position = (int ) (Math .random () * total );
177
+ Log .d (TAG , "pickRandomImage. total images: " + total + ", position: " + position );
178
+ if (total > 0 ) {
179
+ if (c .moveToPosition (position ) ) {
152
180
String data = c .getString (c .getColumnIndex (MediaStore .Images .ImageColumns .DATA ));
153
- uri = Uri .parse ( data );
154
- Log .d ( TAG , uri .toString () );
181
+ uri = Uri .parse (data );
182
+ Log .d (TAG , uri .toString ());
155
183
}
156
184
}
157
185
c .close ();
158
186
}
159
187
return uri ;
160
188
}
161
189
162
- private void loadAsync ( final Uri uri ) {
163
- Log .i ( TAG , "loadAsync: " + uri );
190
+ private void loadAsync (final Uri uri ) {
191
+ Log .i (TAG , "loadAsync: " + uri );
164
192
165
193
Drawable toRecycle = mImage .getDrawable ();
166
- if ( toRecycle != null && toRecycle instanceof BitmapDrawable ) {
167
- if ( ( (BitmapDrawable ) mImage .getDrawable () ).getBitmap () != null )
168
- ( (BitmapDrawable ) mImage .getDrawable () ).getBitmap ().recycle ();
194
+ if (toRecycle != null && toRecycle instanceof BitmapDrawable ) {
195
+ if (( (BitmapDrawable ) mImage .getDrawable ()).getBitmap () != null )
196
+ ((BitmapDrawable ) mImage .getDrawable ()).getBitmap ().recycle ();
169
197
}
170
- mImage .setImageDrawable ( null );
198
+ mImage .setImageDrawable (null );
171
199
mImageUri = null ;
172
200
173
201
DownloadAsync task = new DownloadAsync ();
174
- task .execute ( uri );
202
+ task .execute (uri );
175
203
}
176
204
177
205
class DownloadAsync extends AsyncTask <Uri , Void , Bitmap > implements DialogInterface .OnCancelListener {
@@ -183,61 +211,61 @@ class DownloadAsync extends AsyncTask<Uri, Void, Bitmap> implements DialogInterf
183
211
protected void onPreExecute () {
184
212
super .onPreExecute ();
185
213
186
- mProgress = new ProgressDialog ( MainActivity .this );
187
- mProgress .setIndeterminate ( true );
188
- mProgress .setCancelable ( true );
189
- mProgress .setMessage ( "Loading image..." );
190
- mProgress .setOnCancelListener ( this );
214
+ mProgress = new ProgressDialog (MainActivity .this );
215
+ mProgress .setIndeterminate (true );
216
+ mProgress .setCancelable (true );
217
+ mProgress .setMessage ("Loading image..." );
218
+ mProgress .setOnCancelListener (this );
191
219
mProgress .show ();
192
220
}
193
221
194
222
@ Override
195
- protected Bitmap doInBackground ( Uri ... params ) {
223
+ protected Bitmap doInBackground (Uri ... params ) {
196
224
mUri = params [0 ];
197
225
198
226
Bitmap bitmap = null ;
199
227
200
- while ( mImageContainer .getWidth () < 1 ) {
228
+ while (mImageContainer .getWidth () < 1 ) {
201
229
try {
202
- Thread .sleep ( 1 );
203
- } catch ( InterruptedException e ) {
230
+ Thread .sleep (1 );
231
+ } catch (InterruptedException e ) {
204
232
e .printStackTrace ();
205
233
}
206
234
}
207
235
208
236
final int w = mImageContainer .getWidth ();
209
- Log .d ( TAG , "width: " + w );
210
- bitmap = BitmapLoadUtils .decode (mUri .toString (), imageWidth , imageHeight ,true );
237
+ Log .d (TAG , "width: " + w );
238
+ bitmap = BitmapLoadUtils .decode (mUri .toString (), imageWidth , imageHeight , true );
211
239
return bitmap ;
212
240
}
213
241
214
242
@ Override
215
- protected void onPostExecute ( Bitmap result ) {
216
- super .onPostExecute ( result );
243
+ protected void onPostExecute (Bitmap result ) {
244
+ super .onPostExecute (result );
217
245
218
- if ( mProgress .getWindow () != null ) {
246
+ if (mProgress .getWindow () != null ) {
219
247
mProgress .dismiss ();
220
248
}
221
249
222
- if ( result != null ) {
223
- setImageURI ( mUri , result );
250
+ if (result != null ) {
251
+ setImageURI (mUri , result );
224
252
} else {
225
253
Toast .makeText (MainActivity .this , "Failed to load image " + mUri , Toast .LENGTH_SHORT ).show ();
226
254
}
227
255
}
228
256
229
257
@ Override
230
- public void onCancel ( DialogInterface dialog ) {
231
- Log .i ( TAG , "onProgressCancel" );
232
- this .cancel ( true );
258
+ public void onCancel (DialogInterface dialog ) {
259
+ Log .i (TAG , "onProgressCancel" );
260
+ this .cancel (true );
233
261
}
234
262
235
263
@ Override
236
264
protected void onCancelled () {
237
265
super .onCancelled ();
238
- Log .i ( TAG , "onCancelled" );
266
+ Log .i (TAG , "onCancelled" );
239
267
}
240
268
241
269
}
242
-
270
+
243
271
}
0 commit comments