0

I am trying to open a CSV file downloaded into internal storage for reading purpose alone. This is the code I have used:

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import java.io.FileInputStream;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onClick(View v) {
    try {
        String FileName = "/storage/emulated/0/Download/MyDocument.csv";
        FileInputStream fis = openFileInput(FileName);
        fis.read();
        fis.close();
        Toast.makeText(getBaseContext(),"File Access Permitted",Toast.LENGTH_SHORT).show();

    }

    catch (Exception e) {
        Toast.makeText(getBaseContext(),"File Access Denied",Toast.LENGTH_SHORT).show();
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

} 

While the file is downloaded and I am able to open it in File Manager, the above code does not work. How do I achieve the required functionality?

AR06
  • 161
  • 1
  • 2
  • 16
  • use some good library like http://opencsv.sourceforge.net/ – Haroon May 18 '16 at 06:50
  • How is it not working? Is there an exception, or any kind of output you've done to see if it's working? Include your stack trace if you have one. What is the code for `openFileInput`? – 4castle May 18 '16 at 06:51
  • Yes indeed. Whats not working? Any toasts seen? – greenapps May 18 '16 at 06:55
  • Yes, @greenapps toast is "File Access Denied". It does enter into the try condition (I put the File Access Permitted toast right after the String FileName line), but somehow does not open the required file. – AR06 May 18 '16 at 07:21
  • 1
    So you have an Exception. But you are not toasting `e.getMessage()`. What does it say? Toast it. But you can find it in the LogCat too. – greenapps May 18 '16 at 07:24
  • 1
    `FileInputStream fis = openFileInput(FileName);`. That should be `FileInputStream fis = new FileInputStream(FileName);`. – greenapps May 18 '16 at 07:27
  • You also probably forgot to ask for READ_EXTERNAL_STORAGE permission for your `internal storage`. – greenapps May 18 '16 at 07:29
  • @greenapps, FileInputStream fis = new FileInputStream(FileName); did not work. Toasted e.getMessage(), error states `"/storage/emulated/0/Download/MyDocument.csv: open failed: EACCES (Permission Denied)"`. This is surprising as I have specified ` ` – AR06 May 18 '16 at 08:26
  • Which Android version? – greenapps May 18 '16 at 08:30
  • Thank you so much, @greenapps! Target android version is 21. Turns out that the permissions were in the wrong place. Solved it using [link](http://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android). Should have realized it earlier. – AR06 May 18 '16 at 08:33
  • Please don't use hardcoded paths such as `/storage/emulated/0/Download/` This, in particular, only works in the emulator. – Phantômaxx May 18 '16 at 08:55

1 Answers1

2

As per this stackoverflow question's accepted answer, you could try this:

public String readFileFromDownloads(String fileName) {
    File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    if (!downloadsDir.exists()) return null;

    File file = new File(downloadsDir, fileName);
    if (!file.exists()) return null;

    try {
        StringBuilder fileContent = new StringBuilder();
        BufferedReader br = new BufferedReader(new FileReader(file));

        String line;
        while ((line = br.readLine()) != null) {
            fileContent.append(line);
            fileContent.append('\n');
        }
        br.close();

        return fileContent.toString();
    } catch (IOException ex) {
        //Handle error
        return null;
    }
}

And then call readFileFromDownloads("MyDocument.csv"); from inside your onClick(); method.

Also, you might need to add android.permission.READ_EXTERNAL_STORAGE to your Manifest and handle Android 6.0 new permission system as per Android Docs.

Community
  • 1
  • 1
S. Brukhanda
  • 132
  • 10