Проблема в том, как вы обращаетесь к файлу
String path = String.valueOf(this.getExternalFilesDir(null));
необходимо изменить на Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
Проверьте приведенный ниже пример, он работает
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zackdawood.csvreadwrite">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name=".GenericFileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/external_files"/>
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Data.java
package com.zackdawood.csvreadwrite;
public class Data {
private String PARAM_DISTRICT = "";
private String PARAM_TALUKA = "";
private String PARAM_VILLAGE = "";
private String PARAM_FARMER_NAME = "";
private String PARAM_CONTACT_NUMBER = "";
private String PARAM_GAT_NUMBER = "";
private String PARAM_CROP_NAME = "";
private String PARAM_LANDUSE = "";
private String PARAM_SOIL_TYPE = "";
private String PARAM_TIMESTAMP = "";
private int PARAM_SOIL_DEPTH = 0;
private double PARAM_LATITUDE = 0;
private double PARAM_LONGITUDE = 0;
public Data(String PARAM_DISTRICT, String PARAM_TALUKA, String PARAM_VILLAGE, String PARAM_FARMER_NAME, String PARAM_CONTACT_NUMBER, String PARAM_GAT_NUMBER, String PARAM_CROP_NAME, String PARAM_LANDUSE, String PARAM_SOIL_TYPE, int PARAM_SOIL_DEPTH, double PARAM_LATITUDE, double PARAM_LONGITUDE, String PARAM_TIMESTAMP) {
this.PARAM_DISTRICT = PARAM_DISTRICT;
this.PARAM_TALUKA = PARAM_TALUKA;
this.PARAM_VILLAGE = PARAM_VILLAGE;
this.PARAM_FARMER_NAME = PARAM_FARMER_NAME;
this.PARAM_CONTACT_NUMBER = PARAM_CONTACT_NUMBER;
this.PARAM_GAT_NUMBER = PARAM_GAT_NUMBER;
this.PARAM_CROP_NAME = PARAM_CROP_NAME;
this.PARAM_LANDUSE = PARAM_LANDUSE;
this.PARAM_SOIL_TYPE = PARAM_SOIL_TYPE;
this.PARAM_SOIL_DEPTH = PARAM_SOIL_DEPTH;
this.PARAM_LATITUDE = PARAM_LATITUDE;
this.PARAM_LONGITUDE = PARAM_LONGITUDE;
this.PARAM_TIMESTAMP = PARAM_TIMESTAMP;
}
@Override
public String toString() {
return "Data{" +
"PARAM_DISTRICT='" + PARAM_DISTRICT + '\'' +
", PARAM_TALUKA='" + PARAM_TALUKA + '\'' +
", PARAM_VILLAGE='" + PARAM_VILLAGE + '\'' +
", PARAM_FARMER_NAME='" + PARAM_FARMER_NAME + '\'' +
", PARAM_CONTACT_NUMBER='" + PARAM_CONTACT_NUMBER + '\'' +
", PARAM_GAT_NUMBER='" + PARAM_GAT_NUMBER + '\'' +
", PARAM_CROP_NAME='" + PARAM_CROP_NAME + '\'' +
", PARAM_LANDUSE='" + PARAM_LANDUSE + '\'' +
", PARAM_SOIL_TYPE='" + PARAM_SOIL_TYPE + '\'' +
", PARAM_TIMESTAMP='" + PARAM_TIMESTAMP + '\'' +
", PARAM_SOIL_DEPTH=" + PARAM_SOIL_DEPTH +
", PARAM_LATITUDE=" + PARAM_LATITUDE +
", PARAM_LONGITUDE=" + PARAM_LONGITUDE +
'}';
}
public String getPARAM_DISTRICT() {
return PARAM_DISTRICT;
}
public void setPARAM_DISTRICT(String PARAM_DISTRICT) {
this.PARAM_DISTRICT = PARAM_DISTRICT;
}
public String getPARAM_TALUKA() {
return PARAM_TALUKA;
}
public void setPARAM_TALUKA(String PARAM_TALUKA) {
this.PARAM_TALUKA = PARAM_TALUKA;
}
public String getPARAM_VILLAGE() {
return PARAM_VILLAGE;
}
public void setPARAM_VILLAGE(String PARAM_VILLAGE) {
this.PARAM_VILLAGE = PARAM_VILLAGE;
}
public String getPARAM_FARMER_NAME() {
return PARAM_FARMER_NAME;
}
public void setPARAM_FARMER_NAME(String PARAM_FARMER_NAME) {
this.PARAM_FARMER_NAME = PARAM_FARMER_NAME;
}
public String getPARAM_CONTACT_NUMBER() {
return PARAM_CONTACT_NUMBER;
}
public void setPARAM_CONTACT_NUMBER(String PARAM_CONTACT_NUMBER) {
this.PARAM_CONTACT_NUMBER = PARAM_CONTACT_NUMBER;
}
public String getPARAM_GAT_NUMBER() {
return PARAM_GAT_NUMBER;
}
public void setPARAM_GAT_NUMBER(String PARAM_GAT_NUMBER) {
this.PARAM_GAT_NUMBER = PARAM_GAT_NUMBER;
}
public String getPARAM_CROP_NAME() {
return PARAM_CROP_NAME;
}
public void setPARAM_CROP_NAME(String PARAM_CROP_NAME) {
this.PARAM_CROP_NAME = PARAM_CROP_NAME;
}
public String getPARAM_LANDUSE() {
return PARAM_LANDUSE;
}
public void setPARAM_LANDUSE(String PARAM_LANDUSE) {
this.PARAM_LANDUSE = PARAM_LANDUSE;
}
public String getPARAM_SOIL_TYPE() {
return PARAM_SOIL_TYPE;
}
public void setPARAM_SOIL_TYPE(String PARAM_SOIL_TYPE) {
this.PARAM_SOIL_TYPE = PARAM_SOIL_TYPE;
}
public int getPARAM_SOIL_DEPTH() {
return PARAM_SOIL_DEPTH;
}
public void setPARAM_SOIL_DEPTH(int PARAM_SOIL_DEPTH) {
this.PARAM_SOIL_DEPTH = PARAM_SOIL_DEPTH;
}
public double getPARAM_LATITUDE() {
return PARAM_LATITUDE;
}
public void setPARAM_LATITUDE(double PARAM_LATITUDE) {
this.PARAM_LATITUDE = PARAM_LATITUDE;
}
public double getPARAM_LONGITUDE() {
return PARAM_LONGITUDE;
}
public void setPARAM_LONGITUDE(double PARAM_LONGITUDE) {
this.PARAM_LONGITUDE = PARAM_LONGITUDE;
}
public String getPARAM_TIMESTAMP() {
return PARAM_TIMESTAMP;
}
public void setPARAM_TIMESTAMP(String PARAM_TIMESTAMP) {
this.PARAM_TIMESTAMP = PARAM_TIMESTAMP;
}
}
GenericFileProvider.java
package com.zackdawood.csvreadwrite;
import android.support.v4.content.FileProvider;
public class GenericFileProvider extends FileProvider {
}
strings.xml
<resources>
<string name="app_name">CSVReadWrite</string>
<string name="write_button">Write CSV</string>
<string name="read_button">Read CSV</string>
<string name="request_access">Request Access</string>
</resources>
external_files.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
MainActivity.java
package com.zackdawood.csvreadwrite;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, 112);
Log.v(TAG, "onCreate() Method invoked ");
}
public void read(View view) {
File csvFILE = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Final.csv");
Log.d(TAG, csvFILE.getAbsolutePath());
Uri path = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", csvFILE);
try {
CSVReader csvREAD = new CSVReader(new
FileReader(csvFILE));
String[] csvLINE;
int skip = 0;
try {
Log.d("I was here", "kk");
while ((csvLINE = csvREAD.readNext()) != null) {
if (skip >= 0)//becasue first line is column headers
{
String PARAM_DISTRICT = csvLINE[0];
String PARAM_TALUKA = csvLINE[1];
String PARAM_VILLAGE = csvLINE[2];
String PARAM_FARMER_NAME = csvLINE[3];
String PARAM_CONTACT_NUMBER = csvLINE[4];
String PARAM_GAT_NUMBER = csvLINE[5];
String PARAM_CROP_NAME = csvLINE[6];
String PARAM_LANDUSE = csvLINE[7];
String PARAM_SOIL_TYPE = csvLINE[8];
int PARAM_SOIL_DEPTH = Integer.parseInt(csvLINE[9]);
double PARAM_LATITUDE = Double.parseDouble(csvLINE[10]);
double PARAM_LONGITUDE = Double.parseDouble(csvLINE[11]);
String PARAM_TIMESTAMP = csvLINE[12];
Data STUD_OBJECT = new Data(PARAM_DISTRICT, PARAM_TALUKA, PARAM_VILLAGE,
PARAM_FARMER_NAME, PARAM_CONTACT_NUMBER, PARAM_GAT_NUMBER,
PARAM_CROP_NAME, PARAM_LANDUSE, PARAM_SOIL_TYPE, PARAM_SOIL_DEPTH,
PARAM_LATITUDE, PARAM_LONGITUDE, PARAM_TIMESTAMP
);
//dataArray.add(STUD_OBJECT);
Toast.makeText(this, STUD_OBJECT.toString(), Toast.LENGTH_LONG).show();
} else {
skip++;
}
}
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "read() error" + e.getMessage());
Log.e(TAG, "read() error" + e.getStackTrace());
}
} catch (FileNotFoundException e) {
Log.e(TAG, "read() error" + e.getMessage());
Log.e(TAG, "read() error" + e.getStackTrace());
Toast.makeText(this, "Volley Error", Toast.LENGTH_SHORT).show();
}
}
public void write(View view) {
String Testfile_name = "Final";
Testfile_name = Testfile_name + ".csv";
File logFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), Testfile_name);
if (!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(TAG, "write() error" + e.getMessage());
Log.e(TAG, "write() error" + e.getStackTrace());
}
}
try {
String header = "District,Taluka,Village,Farmer_name,Contact_Number,Gat_Number,Crop_name,Landuse,Soil_Type,Soil_Depth,Latitude,Longitude,Timestamp";
String[] stringList = new String[13];
stringList[0] = "District";
stringList[1] = "Taluka";
stringList[2] = "Village";
stringList[3] = "Farmer_name";
stringList[4] = "Contact_Number";
stringList[5] = "Gat_Number";
stringList[6] = "Crop_name";
stringList[7] = "Landuse";
stringList[8] = "Soil_Type";
stringList[9] = "1";
stringList[10] = "2";
stringList[11] = "2";
stringList[12] = "2007-11-11 12:13:14";
FileWriter fw = new FileWriter(logFile);
CSVWriter writer = new CSVWriter(fw, ',');
writer.writeNext(stringList);
writer.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(TAG, "write() error" + e.getMessage());
Log.e(TAG, "write() error" + e.getStackTrace());
}
}
public void request(View view) {
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, 112);
}
}
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.zackdawood.csvreadwrite"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation "com.opencsv:opencsv:4.0"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="395dp"
android:layout_height="715dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/write_button"
android:onClick="write"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/read_button"
android:onClick="read"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/request_access"
android:onClick="request"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Схема кода
Кодовый вывод