简体   繁体   English

尝试使用Android Studio从内部存储的downloads文件夹中读取.csv文件

[英]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 在此处输入图片说明

Update 更新

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() uridata.getData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从内部存储Android读取文件 - Read file from Internal Storage Android 从内部存储,Android Studio(JAVA)播放音频文件的应用程序 - App that plays audio file from internal Storage, Android Studio (JAVA) 无法从手机内部存储器中的数据文件夹或文档文件夹中读取文本文件 - Can't read text file from the data folder or documents folder in the phone's internal storage Android将JSON文件读/写到内部存储 - Android Read/Write JSON file to Internal Storage 使用Android Studio在内部存储中创建目录和文件夹 - Creating a directory and folder in the internal storage using android studio 需要从android studio中assets文件夹中的txt文件中读取 - Need to read from a txt file in the assets folder in android studio Android从内部存储复制文件到外部 - Android copy file from internal storage to external 在Android中将文件从内部存储复制到外部存储 - Copy file from the internal to the external storage in Android 如何使用 android studio 删除 android 应用程序中的内部存储文件? - How to delete internal storage file in android app using android studio? 从不扩展AppCompatActivity的类从Android Studio上的内部存储打开文件 - Opening a file from internal storage on Android Studio from a class that doesn't extend AppCompatActivity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM