2929import android .provider .DocumentsContract ;
3030import android .provider .MediaStore ;
3131import android .text .TextUtils ;
32- import android .webkit .MimeTypeMap ;
3332import java .io .File ;
3433import java .io .FileOutputStream ;
3534import java .io .IOException ;
@@ -141,7 +140,7 @@ private static String getPathFromRemoteUri(final Context context, final Uri uri)
141140 OutputStream outputStream = null ;
142141 boolean success = false ;
143142 try {
144- String extension = getImageExtension (context , uri );
143+ String extension = getImageExtension (uri );
145144 inputStream = context .getContentResolver ().openInputStream (uri );
146145 file = File .createTempFile ("image_picker" , extension , context .getCacheDir ());
147146 outputStream = new FileOutputStream (file );
@@ -168,31 +167,23 @@ private static String getPathFromRemoteUri(final Context context, final Uri uri)
168167 }
169168
170169 /** @return extension of image with dot, or default .jpg if it none. */
171- private static String getImageExtension (Context context , Uri uriImage ) {
170+ private static String getImageExtension (Uri uriImage ) {
172171 String extension = null ;
173- Cursor cursor = null ;
174172
175173 try {
176- cursor =
177- context
178- .getContentResolver ()
179- .query (uriImage , new String [] {MediaStore .MediaColumns .MIME_TYPE }, null , null , null );
180-
181- if (cursor != null && cursor .moveToNext ()) {
182- String mimeType = cursor .getString (0 );
183-
184- extension = MimeTypeMap .getSingleton ().getExtensionFromMimeType (mimeType );
185- }
186- } finally {
187- if (cursor != null ) {
188- cursor .close ();
174+ String imagePath = uriImage .getPath ();
175+ if (imagePath != null && imagePath .lastIndexOf ("." ) != -1 ) {
176+ extension = imagePath .substring (imagePath .lastIndexOf ("." ) + 1 );
189177 }
178+ } catch (Exception e ) {
179+ extension = null ;
190180 }
191181
192- if (extension == null ) {
182+ if (extension == null || extension . isEmpty () ) {
193183 //default extension for matches the previous behavior of the plugin
194184 extension = "jpg" ;
195185 }
186+
196187 return "." + extension ;
197188 }
198189
0 commit comments