[英]Trying to read a .csv file from downloads folder of internal storage with Android Studio
I am trying to read the contents of a .csv
file which is in my downloads folder of internal storage of my phone. 我正在尝试读取
.csv
文件的内容,该文件位于手机内部存储的downloads文件夹中。 My code looks like this. 我的代码如下所示。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browse = (Button) findViewById(R.id.browse);
editTextPath = (EditText) findViewById(R.id.path);
browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent,"Select file or dir"), 1);
setResult(Activity.RESULT_OK);
Log.e("Content","In onclick");
}
});}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("Content","In onactivity");
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
String path = data.getData().getPath();
Log.e("Pathb",path);
proImportCSV(new File(data.getData().getPath()));
}
}
private void proImportCSV(File file) {
try{
ContentValues cv = new ContentValues();
FileReader fr = new FileReader(file);
CSVReader dataRead = new CSVReader(fr);
String[] vv = null;
while((vv = dataRead.readNext())!=null) {
cv.clear();
}
dataRead.close();
}
catch (Exception e) { Log.e("TAG",e.toString());
}
}
The error I am getting is: 我得到的错误是:
java.io.FileNotFoundException: /document/primary:Download/12132469_About_0_22092018045225.csv: open failed: ENOENT (No such file or directory)
java.io.FileNotFoundException:/document/primary:Download/12132469_About_0_22092018045225.csv:打开失败:ENOENT(没有这样的文件或目录)
I am also sharing a screenshot of my error: 我还分享了我的错误的屏幕截图:
enter image description here 在此处输入图片说明
I made a small change to my previously shared code: 我对以前共享的代码做了一些小的更改:
private void proImportCSV(File file, Uri uri) {
try{
ContentValues cv = new ContentValues();
InputStream inputStream = getContentResolver().openInputStream(uri);
InputStreamReader isr = new InputStreamReader(inputStream);
CSVReader dataRead = new CSVReader(isr);
String[] vv = null;
while((vv = dataRead.readNext())!=null) {
cv.clear();
}
dataRead.close();
}
catch (Exception e) { Log.e("TAG",e.toString());
}
}
Here, uri = data.getData()
. 在这里,
uri = data.getData()
。 After this change I am getting the following error 进行此更改后,出现以下错误
java.io.FileNotFoundException: /storage/emulated/0/Download/12132469_About_0_22092018045225.csv: open failed: EACCES (Permission denied)
java.io.FileNotFoundException:/storage/emulated/0/Download/12132469_About_0_22092018045225.csv:打开失败:EACCES(权限被拒绝)
Also, I have already added this lines of code to my manifest.xml 另外,我已经将这行代码添加到manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You not always can create a working file from a content URI. 您并非总是可以从内容URI创建工作文件。 But you likely don't need it.
但是您可能不需要它。
new CSVReader(fr);
accepts a Reader
as a parameter. 接受
Reader
作为参数。 You can use InputStreamReader
, created from the InputStream
, created from the Uri
using following method: 您可以使用
InputStreamReader
,从创建InputStream
,从创建的Uri
用下面的方法:
InputStream inputStream = getContentResolver().openInputStream(uri);
where uri
is data.getData()
uri
是data.getData()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.