1

I have two android apps, one app accesses the content provider of the other apps to access some file after receiving data from other app. I try to write it to a file but get an exception.

Below is my piece of code

private String getLog(String logFile)
{
    try {
        Uri fileUri = Uri.parse("content://" + AUTHORITY + "/" + logFile);
        ParcelFileDescriptor logFilePath = ControlApplication.getControlApplicationContext()
            .getContentResolver().openFileDescriptor(fileUri, "r");
        InputStream fileIs = new FileInputStream(logFilePath.getFileDescriptor());
        InputStreamReader tmp = new InputStreamReader(fileIs);
        BufferedReader reader = new BufferedReader(tmp);
        String str;
        StringBuffer buf = new StringBuffer();
        while ((str = reader.readLine()) != null) {
            buf.append(str + "\n");
        }
        fileIs.close();
        FileOutputStream outstream = ControlApplication.getControlApplicationContext()
            .openFileOutput(logFile, Context.MODE_PRIVATE);
        outstream.write(buf.toString().getBytes());
        outstream.close();

    }
    catch (Exception e) {
        Maas360Logger.e(loggerName, e, "Exception occured");
    }

    return logFile;
}

Here, logFile is the log file that would already exist from the other app. This file I am trying to get through my content provider.

The exception below is thrown while reading from reader even though I am not creating a duplicate directory:

01-03-2016 19:03:48.265 | E | AsyncTask #9 | DiagnosticServices | Exception occured
java.io.IOException: read failed: EISDIR (Is a directory)

Caused by: read failed: EISDIR (Is a directory)
at libcore.io.IoBridge.read(IoBridge.java:482)
at java.io.FileInputStream.read(FileInputStream.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:231)
at java.io.BufferedReader.fillBuf(BufferedReader.java:145)
at java.io.BufferedReader.readLine(BufferedReader.java:397)
Leon Adler
  • 2,993
  • 1
  • 29
  • 42
TheGraduateGuy
  • 1,450
  • 1
  • 13
  • 35

1 Answers1

1

The problem here is that you're trying to readLine on a directory.

Print out the value of "content://" + AUTHORITY + "/" + logFile and ensure this is what you intend to read.

You can also verify this is a directory like so:

File folder = new File("content://" + AUTHORITY + "/" + logFile");
boolean isDir = folder.isDirectory();

If you want to see what files you should be opening inside this directory, try this answer.

Community
  • 1
  • 1
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159