0

I am trying firstly to download the file and its content from Dropbox in Android app using Dropbox Core API but when i execute the following code the app crushes.

EDIT: I have used two functions downloadDropboxFile and copy functions. The problem is that i am getting blank data when i read the local file which is supposed to contain the dropbox file data.

Here is the code where i call the function

 downloadDropboxFile("/userandpass.txt");

     if (mDBApi.getSession().isLinked())
     {
         InputStream instream = new FileInputStream(String.valueOf(getExternalCacheDir()) + "/userandpass.txt");
         InputStreamReader inputreader = new InputStreamReader(instream);
         BufferedReader buffreader = new BufferedReader(inputreader);
         mTestOutput.setText(buffreader.readLine());
     }

Here is the functions

 private boolean downloadDropboxFile(String fileSelected) {
        File dir = new File(String.valueOf(getExternalCacheDir()));
        if (!dir.exists())
            dir.mkdirs();
        try {
            File localFile = new File(dir + fileSelected);


            if (!localFile.exists()) {
                localFile.createNewFile();
                copy(fileSelected, localFile);

            } else {

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }



    private void copy(final String dbPath, final File localFile) {

        new Thread(new Runnable() {

            @Override
            public void run() {
                BufferedInputStream br = null;
                BufferedOutputStream bw = null;
                try {
                    DropboxAPI.DropboxInputStream fd = mDBApi.getFileStream(dbPath,null);

                    br = new BufferedInputStream(fd);
                    bw = new BufferedOutputStream(new FileOutputStream(localFile));

                    byte[] buffer = new byte[4096];
                    int read;
                    while (true) {
                        read = br.read(buffer);
                        if (read <= 0) {
                            break;
                        }
                        bw.write(buffer, 0, read);
                    }


                } catch (DropboxException e) {
                    e.printStackTrace();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (bw != null) {
                        try {
                            bw.close();
                            if (br != null) {
                                br.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }). start();

    }

Dropbox Core API Implementation on Android Studio:

On app/libs i have the:

dropbox-android-sdk-1.6.3.jar httpmime--4.0.3.jar json_simple-1.1.jar

strategos
  • 119
  • 1
  • 2
  • 6

1 Answers1

0

Your issue is here :

if (!localFile.exists()) {
            localFile.createNewFile(); //otherwise dropbox client will fail silently
}

The exception is :

java.io.IOException: open failed: EROFS (Read-only file system)

This means you're trying to create a File on a location that is read only in the phone's memory, I'm guessing the internal storage. Have a look at this excellent answer by Mark Murphy on creating a File based on locations that can be written to.

Hoping this has been of some help, happy coding ;-)

Community
  • 1
  • 1
2Dee
  • 8,609
  • 7
  • 42
  • 53
  • I have used the cache directory? File dir=new File(String.valueOf(getCacheDir())); File localfile=new File("user.php"); and then just called the function downloadDropboxFile("/user.php",localfile); So this is not allowed? – strategos Mar 04 '15 at 16:18
  • Sorry, missed your question. When you do File localfile=new File("user.php");, that file is not created in the cache folder. See this answer to create a file in the folder you want : http://stackoverflow.com/a/6485850/1178337 – 2Dee Mar 06 '15 at 10:40
  • 2Dee, can you please check my first post which i edited with my new code. I am having headache with downloading the Dropbox file. I am getting only blank data. Thanks in advance. – strategos Mar 06 '15 at 12:37