android studio 内部存储,java - 尝试使用Android Studio从内部存储的downloads文件夹中读取.csv文件 - 堆栈内存溢出...

我正在尝试读取.csv文件的内容,该文件位于手机内部存储的downloads文件夹中。 我的代码如下所示。

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());

}

}

我得到的错误是:

java.io.FileNotFoundException:/document/primary:Download/12132469_About_0_22092018045225.csv:打开失败:ENOENT(没有这样的文件或目录)

我还分享了我的错误的屏幕截图:

更新

我对以前共享的代码做了一些小的更改:

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());

}

}

在这里, uri = data.getData() 。 进行此更改后,出现以下错误

java.io.FileNotFoundException:/storage/emulated/0/Download/12132469_About_0_22092018045225.csv:打开失败:EACCES(权限被拒绝)

另外,我已经将这行代码添加到manifest.xml

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值