Skip to content

Commit 5c88c93

Browse files
committed
Do not resolve filename from Uri
It was observed by one user on another app that uses the same code that the Uri to filename lookup can fail with an IllegalStateException. The code which converts a Uri to a file path may not be that great, especially now that the app is expected to not receive file Uris. Instead of determining the best way to lookup the file names, as it is not that important, the Uri is used directly. This is both for printing the location after the import, and determinimg the mimetype.
1 parent 96716be commit 5c88c93

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

app/src/main/java/protect/budgetwatch/ImportExportActivity.java

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
import android.Manifest;
44
import android.content.ActivityNotFoundException;
5+
import android.content.ContentResolver;
56
import android.content.Context;
67
import android.content.DialogInterface;
78
import android.content.Intent;
89
import android.content.pm.PackageManager;
910
import android.content.pm.ResolveInfo;
10-
import android.database.Cursor;
1111
import android.net.Uri;
1212
import android.os.AsyncTask;
1313
import android.os.Build;
1414
import android.os.Bundle;
1515
import android.os.Environment;
16-
import android.provider.OpenableColumns;
1716
import android.support.annotation.NonNull;
1817
import android.support.v4.app.ActivityCompat;
1918
import android.support.v4.content.ContextCompat;
@@ -25,6 +24,7 @@
2524
import android.util.Log;
2625
import android.view.MenuItem;
2726
import android.view.View;
27+
import android.webkit.MimeTypeMap;
2828
import android.widget.ArrayAdapter;
2929
import android.widget.Button;
3030
import android.widget.Spinner;
@@ -203,21 +203,17 @@ public void onTaskComplete(boolean success)
203203
}
204204
};
205205

206-
String filename = fileNameFromUri(targetUri);
207-
if(filename == null)
208-
{
209-
filename = targetUri.getPath();
210-
}
206+
String mimetype = getMimeType(targetUri);
211207

212-
if(format == null && filename != null)
208+
if(format == null && mimetype != null)
213209
{
214210
// 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);
216212

217213
for(Map.Entry<String, DataFormat> item : _fileFormatMap.entrySet())
218214
{
219-
String key = item.getKey();
220-
if(filename.toLowerCase().endsWith(key.toLowerCase()))
215+
DataFormat value = item.getValue();
216+
if(mimetype.toLowerCase().equals(value.mimetype()))
221217
{
222218
format = item.getValue();
223219
break;
@@ -227,15 +223,15 @@ public void onTaskComplete(boolean success)
227223

228224
if(format != null)
229225
{
230-
Log.d(TAG, "Starting import of file: " + filename);
226+
Log.d(TAG, "Starting import of file");
231227
importExporter = new ImportExportTask(ImportExportActivity.this,
232228
format, target, listener);
233229
importExporter.execute();
234230
}
235231
else
236232
{
237233
// 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");
239235
onImportComplete(false, targetUri);
240236

241237
try
@@ -318,31 +314,39 @@ public boolean onOptionsItemSelected(MenuItem item)
318314
return super.onOptionsItemSelected(item);
319315
}
320316

321-
private String fileNameFromUri(Uri uri)
317+
public String getMimeType(Uri uri)
322318
{
323-
if("file".equals(uri.getScheme()))
324-
{
325-
return uri.getPath();
326-
}
319+
String mimeType = null;
327320

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)
331323
{
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+
}
333333
}
334334

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))
337336
{
338-
returnCursor.close();
339-
return null;
340-
}
337+
ContentResolver cr = getContentResolver();
338+
mimeType = cr.getType(uri);
341339

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+
}
344348

345-
return name;
349+
return mimeType;
346350
}
347351

348352
private void onImportComplete(boolean success, Uri path)
@@ -359,15 +363,10 @@ private void onImportComplete(boolean success, Uri path)
359363
}
360364

361365
int messageId = success ? R.string.importedFrom : R.string.importFailed;
362-
363366
final String template = getResources().getString(messageId);
364367

365368
// 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();
371370

372371
final String message = String.format(template, filename);
373372
builder.setMessage(message);
@@ -495,13 +494,24 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data)
495494

496495
try
497496
{
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+
499508
Log.e(TAG, "Starting file import with: " + uri.toString());
500509
startImport(null, reader, uri);
501510
}
502-
catch (FileNotFoundException e)
511+
catch(FileNotFoundException e)
503512
{
504513
Log.e(TAG, "Failed to import file: " + uri.toString(), e);
514+
onImportComplete(false, uri);
505515
}
506516
}
507517
}

0 commit comments

Comments
 (0)