Skip to content

Commit 98f9e47

Browse files
committed
This importer works with both the system file manager and my third party one
It also works with the Contacts app, which is a mistake... MIME types are annoying
1 parent eebbd36 commit 98f9e47

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

app/src/main/java/com/team980/thunderscout/ximport/CSVImportTask.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
package com.team980.thunderscout.ximport;
2626

2727
import android.app.Activity;
28+
import android.content.ContentResolver;
2829
import android.content.Intent;
30+
import android.content.UriMatcher;
2931
import android.database.Cursor;
3032
import android.database.sqlite.SQLiteDatabase;
3133
import android.database.sqlite.SQLiteException;
34+
import android.net.Uri;
3235
import android.os.AsyncTask;
3336
import android.os.Environment;
3437
import android.provider.Settings;
@@ -54,6 +57,7 @@
5457
import java.io.FileReader;
5558
import java.io.FileWriter;
5659
import java.io.IOException;
60+
import java.io.InputStreamReader;
5761
import java.text.SimpleDateFormat;
5862
import java.util.ArrayList;
5963
import java.util.Date;
@@ -63,19 +67,19 @@ public class CSVImportTask extends AsyncTask<Void, ScoutData, Void> {
6367

6468
private Activity activity;
6569

66-
private File csvFile;
70+
private Uri fileUri;
6771

68-
public CSVImportTask(Activity activity, File file) {
72+
public CSVImportTask(Activity activity, Uri uri) {
6973
this.activity = activity;
70-
csvFile = file;
74+
fileUri = uri;
7175
}
7276

7377
@Override
7478
public Void doInBackground(Void... params) {
7579

7680
CSVReader reader;
7781
try {
78-
reader = new CSVReader(new FileReader(csvFile));
82+
reader = new CSVReader(new InputStreamReader(activity.getContentResolver().openInputStream(fileUri)));
7983
} catch (FileNotFoundException e) {
8084
FirebaseCrash.report(e);
8185
return null;
@@ -107,7 +111,7 @@ public Void doInBackground(Void... params) {
107111
protected void onProgressUpdate(ScoutData[] values) {
108112
//Runs on UI thread when publishProgress() is called
109113

110-
FirebaseCrash.logcat(Log.INFO, this.getClass().getName(), "Posting ScoutData from CSV " + csvFile.getName() + " to database");
114+
FirebaseCrash.logcat(Log.INFO, this.getClass().getName(), "Posting ScoutData from CSV " + fileUri.getLastPathSegment() + " to database");
111115

112116
ScoutDataWriteTask writeTask = new ScoutDataWriteTask(values[0], activity);
113117
writeTask.execute();

app/src/main/java/com/team980/thunderscout/ximport/ImportActivity.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.team980.thunderscout.ximport;
2626

2727
import android.Manifest;
28+
import android.content.ContentResolver;
2829
import android.content.Intent;
2930
import android.content.pm.PackageManager;
3031
import android.net.Uri;
@@ -55,7 +56,7 @@ public class ImportActivity extends AppCompatActivity implements View.OnClickLis
5556

5657
private Button buttonImport;
5758

58-
private File file;
59+
private Uri fileUri;
5960

6061
@Override
6162
protected void onCreate(Bundle savedInstanceState) {
@@ -76,13 +77,13 @@ protected void onCreate(Bundle savedInstanceState) {
7677
public void onClick(View v) {
7778
if (v.getId() == R.id.buttonSelectFile) {
7879
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
79-
intent.setType("file/csv");
80-
Intent i = Intent.createChooser(intent, "File");
80+
intent.setType("*/*");
81+
Intent i = Intent.createChooser(intent, "Open .csv");
8182
startActivityForResult(i, 2);
8283
} else if (v.getId() == R.id.buttonImport) {
8384
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
8485
== PackageManager.PERMISSION_GRANTED) {
85-
CSVImportTask task = new CSVImportTask(this, file);
86+
CSVImportTask task = new CSVImportTask(this, fileUri);
8687
task.execute();
8788
} else {
8889
//Request permission
@@ -101,7 +102,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
101102
if(resultCode == RESULT_OK){
102103
fileInfo.setText(data.getData().getPath());
103104

104-
file = new File(data.getData().getPath());
105+
fileUri = data.getData();
105106

106107
buttonImport.setEnabled(true);
107108
}
@@ -113,7 +114,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
113114
if (requestCode == 1) {
114115
// If request is cancelled, the result arrays are empty.
115116
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
116-
CSVImportTask task = new CSVImportTask(this, file);
117+
CSVImportTask task = new CSVImportTask(this, fileUri);
117118
task.execute();
118119
} else {
119120
//Why would you ever deny the permission?

0 commit comments

Comments
 (0)