Example usage for org.json JSONException initCause

List of usage examples for org.json JSONException initCause

Introduction

In this page you can find the example usage for org.json JSONException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:de.sitewaerts.cordova.documentviewer.DocumentViewerPlugin.java

private File getAccessibleFile(String fileArg) throws JSONException {
    if (fileArg.startsWith(ASSETS)) {
        String filePath = fileArg.substring(ASSETS.length());
        String fileName = filePath.substring(filePath.lastIndexOf(File.pathSeparator) + 1);

        //Log.d(TAG, "Handling assets file: fileArg: " + fileArg + ", filePath: " + filePath + ", fileName: " + fileName);

        try {//from w  ww.ja v  a 2s  .c o  m
            File tmpFile = getSharedTempFile(fileName);
            InputStream in;
            try {
                in = this.cordova.getActivity().getAssets().open(filePath);
                if (in == null)
                    return null;
            } catch (IOException e) {
                // not found
                return null;
            }
            copyFile(in, tmpFile);
            tmpFile.deleteOnExit();
            return tmpFile;
        } catch (IOException e) {
            Log.e(TAG, "Failed to copy file: " + filePath, e);
            JSONException je = new JSONException(e.getMessage());
            je.initCause(e);
            throw je;
        }
    } else {
        File file = getFile(fileArg);
        if (!file.exists() || !file.isFile())
            return null;

        // detect private files, copy to accessible tmp dir if necessary
        // XXX does this condition cover all cases?
        if (file.getAbsolutePath().contains(cordova.getActivity().getFilesDir().getAbsolutePath())) {
            //                  XXX this is the "official" way to share private files with other apps: with a content:// URI. Unfortunately, MuPDF does not swallow the generated URI. :(
            //                  path = FileProvider.getUriForFile(cordova.getActivity(), "de.sitewaerts.cordova.fileprovider", file);
            //                  cordova.getActivity().grantUriPermission(packageId, path, Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            //                  intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

            try {
                File tmpFile = getSharedTempFile(file.getName());
                copyFile(file, tmpFile);
                tmpFile.deleteOnExit();
                return tmpFile;
            } catch (IOException e) {
                Log.e(TAG, "Failed to copy file: " + file.getName(), e);
                JSONException je = new JSONException(e.getMessage());
                je.initCause(e);
                throw je;
            }
        }

        return file;
    }
}