2
2
3
3
import android .Manifest ;
4
4
import android .content .ActivityNotFoundException ;
5
+ import android .content .ContentResolver ;
5
6
import android .content .Context ;
6
7
import android .content .DialogInterface ;
7
8
import android .content .Intent ;
8
9
import android .content .pm .PackageManager ;
9
10
import android .content .pm .ResolveInfo ;
10
- import android .database .Cursor ;
11
11
import android .net .Uri ;
12
12
import android .os .AsyncTask ;
13
13
import android .os .Build ;
14
14
import android .os .Bundle ;
15
15
import android .os .Environment ;
16
- import android .provider .OpenableColumns ;
17
16
import android .support .annotation .NonNull ;
18
17
import android .support .v4 .app .ActivityCompat ;
19
18
import android .support .v4 .content .ContextCompat ;
25
24
import android .util .Log ;
26
25
import android .view .MenuItem ;
27
26
import android .view .View ;
27
+ import android .webkit .MimeTypeMap ;
28
28
import android .widget .ArrayAdapter ;
29
29
import android .widget .Button ;
30
30
import android .widget .Spinner ;
@@ -203,21 +203,17 @@ public void onTaskComplete(boolean success)
203
203
}
204
204
};
205
205
206
- String filename = fileNameFromUri (targetUri );
207
- if (filename == null )
208
- {
209
- filename = targetUri .getPath ();
210
- }
206
+ String mimetype = getMimeType (targetUri );
211
207
212
- if (format == null && filename != null )
208
+ if (format == null && mimetype != null )
213
209
{
214
210
// Attempt to guess the data format based on the extension
215
- Log .d (TAG , "Attempting to determine file type for: " + filename );
211
+ Log .d (TAG , "Attempting to determine file type for: " + mimetype );
216
212
217
213
for (Map .Entry <String , DataFormat > item : _fileFormatMap .entrySet ())
218
214
{
219
- String key = item .getKey ();
220
- if (filename .toLowerCase ().endsWith ( key . toLowerCase ()))
215
+ DataFormat value = item .getValue ();
216
+ if (mimetype .toLowerCase ().equals ( value . mimetype ()))
221
217
{
222
218
format = item .getValue ();
223
219
break ;
@@ -227,15 +223,15 @@ public void onTaskComplete(boolean success)
227
223
228
224
if (format != null )
229
225
{
230
- Log .d (TAG , "Starting import of file: " + filename );
226
+ Log .d (TAG , "Starting import of file" );
231
227
importExporter = new ImportExportTask (ImportExportActivity .this ,
232
228
format , target , listener );
233
229
importExporter .execute ();
234
230
}
235
231
else
236
232
{
237
233
// If format is still null, then we do not know what to import
238
- Log .w (TAG , "Could not import " + filename + ", could not determine extension " );
234
+ Log .w (TAG , "Could not import file because mimetype could not get determined " );
239
235
onImportComplete (false , targetUri );
240
236
241
237
try
@@ -318,31 +314,39 @@ public boolean onOptionsItemSelected(MenuItem item)
318
314
return super .onOptionsItemSelected (item );
319
315
}
320
316
321
- private String fileNameFromUri (Uri uri )
317
+ public String getMimeType (Uri uri )
322
318
{
323
- if ("file" .equals (uri .getScheme ()))
324
- {
325
- return uri .getPath ();
326
- }
319
+ String mimeType = null ;
327
320
328
- Cursor returnCursor =
329
- getContentResolver ().query (uri , null , null , null , null );
330
- if (returnCursor == null )
321
+ String fileExtension = MimeTypeMap .getFileExtensionFromUrl (uri .toString ());
322
+ if (fileExtension != null )
331
323
{
332
- return null ;
324
+ fileExtension = fileExtension .toLowerCase ();
325
+ for (DataFormat format : DataFormat .values ())
326
+ {
327
+ if (fileExtension .equals (format .name ().toLowerCase ()))
328
+ {
329
+ mimeType = format .mimetype ();
330
+ break ;
331
+ }
332
+ }
333
333
}
334
334
335
- int nameIndex = returnCursor .getColumnIndex (OpenableColumns .DISPLAY_NAME );
336
- if (returnCursor .moveToFirst () == false )
335
+ if (mimeType == null && uri .getScheme () != null && uri .getScheme ().equals (ContentResolver .SCHEME_CONTENT ))
337
336
{
338
- returnCursor .close ();
339
- return null ;
340
- }
337
+ ContentResolver cr = getContentResolver ();
338
+ mimeType = cr .getType (uri );
341
339
342
- String name = returnCursor .getString (nameIndex );
343
- returnCursor .close ();
340
+ if (mimeType != null )
341
+ {
342
+ if (mimeType .equals ("text/comma-separated-values" ))
343
+ {
344
+ mimeType = "text/csv" ;
345
+ }
346
+ }
347
+ }
344
348
345
- return name ;
349
+ return mimeType ;
346
350
}
347
351
348
352
private void onImportComplete (boolean success , Uri path )
@@ -359,15 +363,10 @@ private void onImportComplete(boolean success, Uri path)
359
363
}
360
364
361
365
int messageId = success ? R .string .importedFrom : R .string .importFailed ;
362
-
363
366
final String template = getResources ().getString (messageId );
364
367
365
368
// Get the filename of the file being imported
366
- String filename = fileNameFromUri (path );
367
- if (filename == null )
368
- {
369
- filename = "(unknown)" ;
370
- }
369
+ String filename = path .toString ();
371
370
372
371
final String message = String .format (template , filename );
373
372
builder .setMessage (message );
@@ -495,13 +494,24 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data)
495
494
496
495
try
497
496
{
498
- InputStream reader = getContentResolver ().openInputStream (uri );
497
+ InputStream reader ;
498
+
499
+ if (uri .getScheme () != null )
500
+ {
501
+ reader = getContentResolver ().openInputStream (uri );
502
+ }
503
+ else
504
+ {
505
+ reader = new FileInputStream (new File (uri .toString ()));
506
+ }
507
+
499
508
Log .e (TAG , "Starting file import with: " + uri .toString ());
500
509
startImport (null , reader , uri );
501
510
}
502
- catch (FileNotFoundException e )
511
+ catch (FileNotFoundException e )
503
512
{
504
513
Log .e (TAG , "Failed to import file: " + uri .toString (), e );
514
+ onImportComplete (false , uri );
505
515
}
506
516
}
507
517
}
0 commit comments